#include<stdio.h>
#include<stdlib.h>
main ()
{
int p,q,m,n,i,j,k,s=0;
int a[20][20],b[20][20],c[20][20];
printf("Enter the Order of First Matrix\t\t");
scanf("%d%d",&m,&n);
printf("Enter the Order of Second Matrix\t");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrices are not Compatible for Multiplication");
exit(0);
}
printf("Enter the First Matrix\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the Second Matrix\t");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
s=s=a[i][k]*b[k][j];
c[i][j]=s;
s=0;
}
}
printf("Product Of Given Matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
LOGIC REFERENCE(WIKIPEDIA):Matrix Multiplication
#include<stdlib.h>
main ()
{
int p,q,m,n,i,j,k,s=0;
int a[20][20],b[20][20],c[20][20];
printf("Enter the Order of First Matrix\t\t");
scanf("%d%d",&m,&n);
printf("Enter the Order of Second Matrix\t");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrices are not Compatible for Multiplication");
exit(0);
}
printf("Enter the First Matrix\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the Second Matrix\t");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
s=s=a[i][k]*b[k][j];
c[i][j]=s;
s=0;
}
}
printf("Product Of Given Matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
![]() |
| output |
LOGIC REFERENCE(WIKIPEDIA):Matrix Multiplication

No comments:
Post a Comment