Home C Language Basic C Programs C Program for Matrix Multiplication

C Program for Matrix Multiplication

Write a program in C for multiplication of two square Matrices.

#include<stdio.h>

int main()

{

int m1,n1,m2,n2;

int a[10][10],b[10][10],c[10][10],i,j,k;

printf(“Enter size of 1st matrix:\n”);

scanf(“%d%d”,&m1,&n1);

printf(“Enter size of 2nd matrix:\n”);

scanf(“%d%d”,&m2,&n2);

if(m2!=n1)

{

printf(“Multiplication is not possible for these matrices\n”);

return 0;

}

printf(“Enter First Matrix:\n”);

for(i=0;i<m1;i++)

for(j=0;j<n1;j++)

scanf(“%d”,&a[i][j]);

printf(“Enter Second Matrix:\n”);

for(i=0;i<m2;i++)

for(j=0;j<n2;j++)

scanf(“%d”,&b[i][j]);

printf(“Multiplication of two Matrices is:\n”);

for(i=0;i<m1;i++)

{

for(j=0;j<n2;j++)

{

c[i][j]=0;

for(k=0;k<n1;k++)

c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

printf(“%d\t”,c[i][j]);

}

printf(“\n”);

}

return 0;

}

Output:

Enter size of 1st matrix:

3              3

Enter size of 2nd matrix:

3              3

Enter First Matrix:

2              2              2              2              2              2              2              2              2

Enter Second Matrix:

1              2              3              4              3              2              1              0              2

Multiplication of two Matrices is:

12           10           14

12           10           14

12           10           14