C Programming

C Program – Multiplication of two Matrices Example

Here we are going to solve multiplication of two matrices using C Program.

Multiplication of two Matrices :

ArrayMultiplication.c

#include
#define ROW1 3
#define COL1 4
#define ROW2 COL1
#define COL2 3
int main(void) {
    int i, j, k;
    int matx1[ROW1][COL1], matx2[ROW2][COL2], matx3[ROW1][COL2];
    printf("Enter Matrix 1 (%dx%d) :\n", ROW1, COL1);
    for (i = 0; i < ROW1; i++) {
        for (j = 0; j < COL1; j++) {
            scanf("%d", &matx1[i][j]);
        }
    }

    printf("Enter Matrix 2 (%dx%d) :\n", ROW2, COL2);
    for (i = 0; i < ROW2; i++) {
        for (j = 0; j < COL2; j++) {
            scanf("%d", &matx2[i][j]);
        }
    }

    /*Multiplication Logic Here*/
    for (i = 0; i < ROW1; i++) {
        for (j = 0; j < COL2; j++) {
            matx3[i][j] = 0;
            for (k = 0; k < COL1; k++){
                matx3[i][j] += matx1[i][k] * matx2[k][j];
            }
        }
    }

    printf("The Resultant Matrix is :\n");
    for (i = 0; i < ROW1; i++) {
        for (j = 0; j < COL2; j++)
            printf("%5d", matx3[i][j]);
        printf("\n");
    }
    return 0;
}

Output:

Terminal
Enter Matrix 1 (3x4) :
1 2 3 4
7 8 9 4
9 8 7 5
Enter Matrix 2 (4x3) :
4 5 6
7 8 9
1 2 3
3 6 9
The Resultant Matrix is :
33   51   69
105  141  177
114  153  192

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

NumPy

NumPy Matrix Multiplication Example

In this tutorial, we will see how to do Numpy Matrix Multiplication using NumPy library. NumPy Multiplication: Let's say we have two 2-d arrays say arr1 and arr2,…

Ask a question or leave a comment