Matrix Multiplication

Algorithm

Step 1: Start

Step 2: Declare matrices mat1, mat2, and result

Step 3: Initialize matrices mat1 and mat2

Step 4: Multiply mat1 and mat2 using nested loops

Step 5: Store the result in result matrix

Step 6: Print the result matrix

Step 7: End

Flowchart

Flowchart

Program


#include <stdio.h>
int main()
{
    int mat1[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int mat2[3][2] = {{1, 2}, {3, 4}, {5, 6}};
    int result[2][2] = {0};
    
    printf("Matrix 1:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", mat1[i][j]);
        }
        printf("\n");
    }
    
    printf("\nMatrix 2:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", mat2[i][j]);
        }
        printf("\n");
    }
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 3; k++) {
                result[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    }
    
    printf("\nResultant Matrix:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
    return 0;
}
                
Test Case Output
Matrix 1:
1 2 3
4 5 6

Matrix 2:
1 2
3 4
5 6

Resultant Matrix:
22 28
49 64