C program to multiply two matrix

      No Comments on C program to multiply two matrix

Multiply two matrix

This program illustrate the concept to multiply two matrix.

  • In this program “m=row” and “n=coloumn” for first matrix.
  • And “p=row” and “q=coloumn” for second matrix.
  • We can multiply two matrix if and only if rows of first matrix=coloumns of second matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[15][15],b[15][15],c[15][15],i,j,e,m,n,p,q,sum=0;
 clrscr();
 printf("Enter rows and coloumn of first matrix");
 scanf("%d%d",&m,&n);
 printf("Enter First matrix row wise:-\n");
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     scanf("%d",&a[i][j]);
    }
  }
   printf("Enter rows and coloumn of second matrix");
   scanf("%d%d",&p,&q);
  printf("Enter Second matrix row wise:-\n");
  for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     scanf("%d",&b[i][j]);
    }
  }
 if(n==p)
 {
 for(i=0;i<m;i++)
   {
    for(j=0;j<q;j++)
     {
       for(e=0;e<p;e++)
       {
	sum=sum+(a[i][e]*b[e][j]) ;

      }
      c[i][j]=sum;
      sum=0;
     }
   }

 }
 else
 {
  printf("Matrix multiplication is not possible ");
 }
 printf("Product of two matrix is :- \n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<q;j++)
  {
   printf(" %d",c[i][j]);
  }
   printf("\n");
 }
 getch();
}

OUTPUT

If their is only one row and coloumn

multiply two matrix

If their are 3 rows and 3 coloums

multiply two matrix


What is matrix

  • It is a rectangular array arranged in rows and coloumns.
  • Suppose a matrix “A” is denoted by m x n order.Where m=rows and n=coloumns.
  • Individual item in matrix “A” will be denoted by “aij” where i=m and j=n.

How matrix multiplication takes place

If their is only one row and one coloumn

Multiply two matrice


Comment below if you are facing any problem


Also read

How to add two matrix in c program
How to transpose a entered matrix in c program

Leave a Reply

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