C Online Compiler
Example: 2x2 Matrix Multiplication in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 2x2 Matrix Multiplication #include <stdio.h> int main() { int A[2][2] = {{1, 2}, {3, 4}}; // First 2x2 matrix int B[2][2] = {{5, 6}, {7, 8}}; // Second 2x2 matrix int C[2][2]; // Resultant 2x2 matrix int i, j, k; // Step 1: Initialize the resultant matrix C with zeros for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { C[i][j] = 0; } } // Step 2: Perform matrix multiplication // Outer loops iterate through rows of A (i) and columns of B (j) for (i = 0; i < 2; i++) { // Iterate through rows of resultant matrix C for (j = 0; j < 2; j++) { // Iterate through columns of resultant matrix C for (k = 0; k < 2; k++) { // Iterate through columns of A / rows of B C[i][j] += A[i][k] * B[k][j]; } } } // Step 3: Print the resultant matrix C printf("Resultant Matrix C:\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { printf("%d ", C[i][j]); } printf("\n"); // New line after each row } return 0; }
Output
Clear
ADVERTISEMENTS