Header Ads Widget

Write a program to print the multiplication of two matrix.

 Code:-

#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,x;
int r1,r2,c1,c2;
printf("Enter the number of row and column of first matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter the number of row and column of second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2) // necessary condition for matrix multiplication
printf("multiplication is not possible\n");
else
{
printf("Enter the element of the first matrix\n");
// Input matrix a[][]
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the element of the second matrix\n");
// Input matrix b[][]
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
// Logic for multiplication
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(x=0;x<c1;x++)
{
c[i][j]=c[i][j]+a[i][x]*b[x][j];
}
}
}
//Display matrix
printf("multiplication of the matrix is:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
return 0;
}

Output:-


Enter the number of row and column of first matrix
2 3
Enter the number of row and column of second matrix
3 4
Enter the element of first matrix
1 2 3
3 5 6
Enter the element of second matrix
1 3 5 2
4 3 9 0
0 1 2 3
Multiplication of the matrix is:-
9 12 29 11
23 30 72 24