#include <stdio.h>
void main()
{
int m,n,p,q,c,d,k,sum=0;
int f[10][10],s[10][10],mul[10][10];
printf("Enter the number of rows and columns of First Matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of First Matrix\n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&f[c][d]);
printf("Enter the number of rows and columns of Second Matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("The matrices multiplication is not possible.\n");
else
{
printf("Enter the elements of Second Matrix\n");
for(c=0;c<p;c++)
for(d=0;d<q;d++)
scanf("%d",&s[c][d]);
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
{
for(k=0;k<p;k++)
{
sum=sum+f[c][k]*s[k][d];
}
mul[c][d]=sum;
sum = 0;
}
}
printf("Product of Matrices is:\n");
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
printf("%d\t", mul[c][d]);
printf("\n");
}
}
}
Output 1:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of Second Matrix
1
2
3
4
Product of Matrices is:
7 10
15 22
void main()
{
int m,n,p,q,c,d,k,sum=0;
int f[10][10],s[10][10],mul[10][10];
printf("Enter the number of rows and columns of First Matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of First Matrix\n");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&f[c][d]);
printf("Enter the number of rows and columns of Second Matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("The matrices multiplication is not possible.\n");
else
{
printf("Enter the elements of Second Matrix\n");
for(c=0;c<p;c++)
for(d=0;d<q;d++)
scanf("%d",&s[c][d]);
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
{
for(k=0;k<p;k++)
{
sum=sum+f[c][k]*s[k][d];
}
mul[c][d]=sum;
sum = 0;
}
}
printf("Product of Matrices is:\n");
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
printf("%d\t", mul[c][d]);
printf("\n");
}
}
}
Output 1:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of Second Matrix
1
2
3
4
Product of Matrices is:
7 10
15 22