Write a program to multiply two matrices of size M x N and N x P respectively.
Answers
#include<stdio.h>
#include<conio.h>
#define ROW 20
#define COL 20
void main()
{
int a[ROW][COL],b[ROW][COL],c[ROW][COL];
int i=0,j=0,k,sum=0,m,n;
clrscr();
printf("\n enter the m\n");
scanf("%d",&m);
printf("\n enter the n\n");
scanf("%d",&n);
printf("\n enter the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n enter the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("\n the product of matrices is as follow \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}