multiply two given matrix
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("\n enter the no. of rows the matrix A:");
scanf("%d",&m);
printf("\n enter the no. of columns the matrix A:");
scanf("%d",&n);
printf("\n enter the no. of rows the matrix B:");
scanf("%d",&p);
printf("\n enter the no. of columns the matrix B:");
scanf("%d",&q); /* check for the condition of multiplication */
if(n==p)
{
printf("\n The two matrices can be multiplied ");
for(i=0;i<m;i++) /* input matrix A */
for(j=0;j<n;j++)
{
printf("\n enter the a[%d][%d] element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* enter the matric B */
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\n enter the b[%d][%d] element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
/* printing the matric A */
printf("\n The elements of the given matrix A are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
}
/* printing the matric B */
printf("\n The elements of the given matrix B are:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",b[i][j]);
}
for(i=0;i<m;i++) /* multipling the two matrices */
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
/* printing the multiplication of the given matrices */
printf("\n The resultant matrix is of order %d*%d",m,q);
printf("\n The multiplication of the given matrices is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",c[i][j]);
}
}
else
printf("\n The two matrices can not be multiplied ");
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("\n enter the no. of rows the matrix A:");
scanf("%d",&m);
printf("\n enter the no. of columns the matrix A:");
scanf("%d",&n);
printf("\n enter the no. of rows the matrix B:");
scanf("%d",&p);
printf("\n enter the no. of columns the matrix B:");
scanf("%d",&q); /* check for the condition of multiplication */
if(n==p)
{
printf("\n The two matrices can be multiplied ");
for(i=0;i<m;i++) /* input matrix A */
for(j=0;j<n;j++)
{
printf("\n enter the a[%d][%d] element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* enter the matric B */
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\n enter the b[%d][%d] element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
/* printing the matric A */
printf("\n The elements of the given matrix A are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
}
/* printing the matric B */
printf("\n The elements of the given matrix B are:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",b[i][j]);
}
for(i=0;i<m;i++) /* multipling the two matrices */
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
/* printing the multiplication of the given matrices */
printf("\n The resultant matrix is of order %d*%d",m,q);
printf("\n The multiplication of the given matrices is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",c[i][j]);
}
}
else
printf("\n The two matrices can not be multiplied ");
getch();
}
No comments:
Post a Comment