Multiplication of two matrix – C Program

Matrix Multiplication – C Program

write a c program to multiplication of two matrix.

 #include<stdio.h>
#include<conio.h>

void main()
{
   int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
   printf("\nEnter the row and column of first matrix :\n");
   scanf("%d %d",&m,&n);
   printf("\nEnter the row and column of second matrix :\n");
   scanf("%d %d",&o,&p);
   if(n!=o)
   {
      printf("Matrix mutiplication is not possible");
      printf("\nColumn of first matrix must be same as row of second matrix");
   }
  else
   {
      printf("\nEnter the First matrix: \n");
      for(i=0;i<m;i++)
      {
      for(j=0;j<n;j++)
        {
           scanf("%d",&a[i][j]);
        }
       }

      printf("\nEnter the Second matrix :\n");
      for(i=0;i<o;i++)
      {
       for(j=0;j<p;j++)
        {      
         scanf("%d",&b[i][j]);
        }
      }

   for (i = 0; i < m; i++)
     {
      for (j = 0; j < p; j++) 
       {
        for (k = 0; k < o; k++)
         {
          sum = sum + first[i][k]*second[k][j];
          }
          multiply[i][j] = sum;
          sum = 0;
       }
     }

  }

  printf("\nThe multiplication of two matrix is :\n");
  for(i=0;i<m;i++)
      {
        printf("\n");
        for(j=0;j<p;j++)
          {
           printf("%d\t",c[i][j]);
          }
      } 
}

lets understand multiplication of matrix

matrix A=  1    2           matrix B = 5   6   7

.                3    4                           8   9   10

The multiplication of two matrix:

A * B =  1*5  +  2*8          1*6  +  2*9         1*7  + 2*10

.            3*5  +  4*8          3*6  +  4*9         3*7  + 4*10

 

A * B =  21       24       27

.            47      54        61

 

The output of matrix multiplication is :

Enter the row and column of first matrix :
3 3

Enter the row and column of second matrix :
3 3

Enter the First matrix :
1  2  0
0  1  1
2  0  1

Enter the Second matrix :
1  1  2
2  1  1
1  2  1

The multiplication of two matrix is :
5  3  4
3  3  2
3  4  5

 

Author: Meera Academy

Leave a Reply

Your email address will not be published. Required fields are marked *