C++ program to multiply two entered matrix

Multiply two entered matrix

This program illustrate the concept of multiply two entered matrix.

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

#include<iostream.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();
 cout<<"Enter rows and coloumns of first matrix"<<endl;
 cin>>m>>n;
 cout<<"Enter First matrix row wise:-\n";
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     cin>>a[i][j];
    }
  }
   cout<<"Enter rows and coloumns of second matrix"<<endl;
   cin>>p>>q;
  cout<<"Enter Second matrix row wise:-\n";
  for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cin>>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;
     }
   }
 cout<<"Product of two matrix is :- \n";
 for(i=0;i<m;i++)
 {
  for(j=0;j<q;j++)
  {
   cout<<"\t"<<c[i][j];
  }
   cout<<"\n";
 }

 }
 else
 {
  cout<<"Matrix multiplication is not possible ";
 }

 getch();
}

OUTPUT

If there are equal rows and coloumns.

multiply two entered matrix


If rows and coloumns are not equal.

multiply two entered matrix


How matrix multiplication takes place

If there is only one row and coloumn

multiply two entered matrix


Comment below if you are facing any problem


Also read

C++ program to show addition and subtraction of two matrix
C++ program to show matrix transpose
C program to multiply two matrix

Leave a Reply

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