Programs to multiply two Matrices in C

Programs to multiply two Matrices

The multiplication of matrices is possible only if the following condition satisfy that is if the matrix one has A rows and B columns and matrix two has C rows and D columns then B columns must be equal to C rows  ( B = C ) 

Program:

/* c program for matrix multiplication*/

#include <stdio.h>
#include <conio.h>
int main( )
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q,k;
clrscr( );
printf(“\nEnter the size of first matrices: ”);
scanf(“%d%d’, &m, &n);
printf(“\nEnter the size of second matrix: ”);
scanf(“%d%d’, &p, &q);
 if(n == p)
 {
   printf(“\nEnter the elements of 1st matrix: ”);
   for(i = 1; i < m; i ++)
    for(j = 1; j < n; j ++)
    scanf(“%d”,&a[i][j]);
    printf(“\nEnter the elements of 2nd matrix: ”);
    for(i = 1;i < p;i ++)
     for(j = 1;j < q;j ++)
      scanf(“%d”, &b[i][j]);
     for(i = 1; i < m; i ++)
      for(j = 1; j < n; j ++)
     {
      c[i][j] = 0;
        for(k = 1;k < n;k ++)
         c[i][j] = c[i][j] + a[i][k] * b[k][j];
     }
   printf(“\nThe multiplication matrix's is:\n ”);
   for(i = 1;i < m;i ++)
   {
    for(j = 1;j < n;j ++)
     print(“%2d”, c[i][j]);
    printf(“\n”);
   }
 }
else
printf(“\nMultiplication is not possible”);
return 0;
}

Output:

Enter the size of first matrices: 2 
2
Enter the size of second matrix: 2 
2
Enter the elements of 1st matrix: 1
2
3
4
Enter the elements of 2nd matrix: 5
6
7
8
The multiplication matrix's is: 
  19   22
  43   50

By using the above program we can multiply the matrices 

Post a Comment

0 Comments