#include<stdio.h>
#include<math.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n,p,q,k;
printf("Enter no. of rows & colomns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter no. of rows & colomns of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible");
exit(0);
}
printf("Enter elements into first matrix\n");
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements into second matrix\n");
for(i=1; i<=p; i++)
{
for(j=1; j<=q; j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1; i<=m; i++)
{
for(j=1; j<=q; j++)
{
c[i][j]=0;
for(k=1;k<=p;k++)
{
c[i][j]= c[i][j] + a[i][k]*b[k][j];
}
}
}
printf("The Matrix multiplication is:\n");
for(i=1; i<=m; i++)
{
for(j=1; j<=q; j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Note: Need to be arranged in compiler after copied
OutPut:
Enter no. of rows & colomns of first matrix
2
2
Enter no. of rows & colomns of second matrix
2
3
Enter elements into first matrix
1
2
3
4
Enter elements into second matrix
1
2
3
4
5
6
The Matrix multiplication is:
9 12 15
19 26 33