Thursday, April 3, 2014

PROGRAM TO MULTIPLY TWO MATRICES

//PROGRAM TO MULTIPLY TWO MATRICES
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],k,i,j,r1,c1,r2,c2;
clrscr();
printf("enetr the no of rows of first matrics\n");
scanf("%d",&r1);
printf("enter the no of cols of first matrics\n");
scanf("%d",&c1);
printf("enetr the no of rows of second matrics\n");
scanf("%d",&r2);
printf("enter the no of cols of second matrics\n");
scanf("%d",&c2);
printf("enter the elements of first matrics\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("the elements of first matrics are \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}
printf("enter the elements of second matrics\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("the elements of second matrics are \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf(" %d ",b[i][j]);
printf("\n\n");
}

if(c1==r2)
{
printf("the multiplication of two matrices is\n");
for(i=0;i<r1;i++)
{
 for(j=0;j<c2;j++)
   {
     c[i][j]=0;
      for(k=0;k<c1;k++)
       {
        c[i][j]=c[i][j]+a[i][k]*b[k][j];
       }
     printf(" %d ",c[i][j]);
   }
  printf("\n\n");
}
}
else
printf("multiplication is not possible\n");

getch();
}

No comments:

Post a Comment