PROBLEM-16
Write a C-program to find the product of any two matrices .
Answer :
//Program to find the product of two matrices
//Bangabasi College, Dept. of Mathematics
#include<stdio.h>
int main()
{
int i,j,m,n,p,q,k;
float a[10][10],b[10][10],c[10][10];
printf("Enter the number of rows and columns of 1st matrix respectively:\t");
scanf("%d %d",&m,&n);
printf("Enter the number of rows and columns of 2nd matrix respectively:\t");
scanf("%d %d",&p,&q);
if(n!=p)
printf("Product of these two matrices is not defined");
else
{
printf("Product of these two matrices is defined\n");
printf("\nEnter the elements of 1st matrix row-wise:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}
printf("\nEnter the elements of 2nd matrix row-wise:\n");
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
scanf("%f",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0.0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
printf("\nProduct of these two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%f\t",c[i][j]);
printf("\n");
}
}
return 0;
}