In this program, we are going to share a C program for matrix multiplication. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program for matrix multiplication with the output.
Copy the below C program and execute it with the help of Turbo C compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int m1[10][10], m2[10][10], m3[10][10]; printf("Please enter the number of rows of 1st matrix\n"); scanf("%d", &m); printf("Please enter number of columns of 1st matrix\n"); scanf("%d", &n); printf("Please enter the elements of 1st matrix one by one\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) { scanf("%d", &m1[c][d]); } } printf("Please enter the number of rows of 2nd matrix\n"); scanf("%d", &p); printf("Please enter number of columns of 2nd matrix\n"); scanf("%d", &q); if ( n != p ) { printf("The entered matrices can't be multiplied each other.\n"); printf("To multiply two matrices of X and Y, the number of columns in X must be equal to the number of rows in Y"); } else { printf("Please enter the elements of 2nd matrix one by one\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &m2[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + m1[c][k]*m2[k][d]; } m3[c][d] = sum; sum = 0; } } printf("Multiplication of entered matrices:\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", m3[c][d]); printf("\n"); } } return 0; } |
Please enter the number of rows of 1st matrix
3
Please enter number of columns of 1st matrix
3
Please enter the elements of 1st matrix one by one
1
2
3
1
2
3
1
2
3
Please enter the number of rows of 2nd matrix
3
Please enter number of columns of 2nd matrix
3
Please enter the elements of 2nd matrix one by one
4
5
6
4
5
6
4
5
6
Multiplication of entered matrices:
24 30 36
24 30 36
24 30 36
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: C program to multiply two matrices, C Program to Multiply two Matrices by Passing Matrix to a Function, C Program to Perform Matrix Multiplication, Matrix multiplication in C, Program to multiply two matrices, Simple C Program for Matrix Multiplication