C Program – Multiplication of two Matrices Example

By | 2017-09-17T10:38:13+00:00 September 17th, 2017|c-program|0 Comments

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

Multiplication of two Matrices :

#include<stdio.h>
#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;
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Enter Matrix 1 (3×4) :
1 2 3 4
7 8 9 4
9 8 7 5
Enter Matrix 2 (4×3) :
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

[/box]

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment