Program Discussion :: Array
10 / 30
Write a logic for Matrix multiplication.
Answer:
#include
int main()
{
int m,n,i,j,p,q,k,s=0;
int first[50][50],second[50][50],third[50][50];
printf("\nEnter the no. of rows and coloumns of first matrix: ");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of first matrix: ");
for(i=0;i
Asked In ::
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
#include<stdio.h>
int main()
{
int m,n,i,j,p,q,k,s=0;
int first[50][50],second[50][50],third[50][50];
printf("\nEnter the no. of rows and coloumns of first matrix: ");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of first matrix: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&first[i][j]);
}
}
printf("\nEnter the no. of rows and coloumns of second matrix: ");
scanf("%d%d",&p,&q);
if(n!=p)
printf("\nMatrix multiplication not possible!!");
else
{
printf("\nEnter the elements of second matrix: ");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&second[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
s=s+first[i][k]*second[k][j];
}
third[i][j]=s;
s=0;
}
}
printf("\nProduct of the 2 matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",third[i][j]);
}
printf("\n");
}
}
}