Pages

Sunday, November 15, 2015

C Program for Multiplication(Product) of matrices(Matrix)

Program

#include <stdio.h>

void main()
{
  int row,col,r1,c1,r2,c2,k;
  int first[10][10],second[10][10],multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d",&r1,&c1);
  printf("Enter the elements of first matrix\n");

  for(row=0;row<r1;row++)
for(col=0;col<c1;col++)
scanf("%d", &first[row][col]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d",&r2,&c2);

if(c1!=r2)
printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
printf("Enter the elements of second matrix\n");

for(row=0;row<r2;row++)
for(col=0;col<c2;col++)
scanf("%d", &second[row][col]);

for(row=0;row<r1;row++)
{
for(col=0;col<c2;col++)
{
multiply[row][col]=0;

for(k=0;k<r2;k++)
multiply[row][col]+= first[row][k]*second[k][col];
}
}
printf("Product of entered matrices:-\n");

for(row=0;row<r1;row++)
{
for(col=0;col<c2;col++)
 printf("%d\t", multiply[row][col]);

printf("\n");
}
}

}

Output

Enter the number of rows and columns of first matrix
2
3
Enter the elements of first matrix
1 2 3
4 5 6
Enter the number of rows and columns of second matrix
3
2
Enter the elements of second matrix
1 2
3 4
5 6
Product of entered matrices:-
22      28
49      64

No comments:

Post a Comment