c program to find the product of two matrices,write a program to multiply two matrices,Write a program to find the product of two matrices in c
Write a c program to find the product of two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],p[10][10],i,j,k,r1,c1,r2,c2;
clrscr();
printf("Enter the order of the first matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter the order of the second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("Enter
the elements of first matrix\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter
the elements of second matrix\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
p[i][j]=0;
for(k=0;k<c1;k++)
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
printf("The resultant matrix is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
}
else
printf("Matrices are not compatible");
getch();
}
/*Output:
run 1:
Enter the order of the first matrix
2
2
Enter the order of the second matrix
2
2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
7 8
The resultant matrix is:
19 22
43 50
run 2:
Enter the order of the first matrix
2
2
Enter the order of the second matrix
3
3
No comments: