Monday , October 23 2017
Home / c-program / C Program – Multiplication of two Matrices Example

C Program – Multiplication of two Matrices Example

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;
}

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

Happy Learning 🙂

About chandrashekhar

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+ ,

Recommended

C Program – Find Largest and Smallest number in an Array

C Program to find the largest and smallest number in an array. Largest and Smallest …

Leave a Reply

Your email address will not be published. Required fields are marked *