Program to find the product of two matrices

Tuesday, August 11, 2020

Learn C++ (Introduction and Tutorials to C++ Programming)

//Program to find product of two matrices

#include<iostream.h>

#include<conio.h>

void main()

{

 clrscr();

 int A[10][10],B[10][10],C[10][10],m,n,p,q,i,k,j;

 cout<<"\n enter the size of first matrix :";

 cin>>m>>n;

 cout<<"\n enter the size of second matrix :";

 cin>>p>>q;

 if(n==p)

 {

  cout<<"\n product of two matrix is possible ";

  cout<<"\n enter the elements of first matrix ";

  {

   for(i=0;i<m;i++)

   for(j=0;j<n;j++)

   {

    cout<<"\n enter A["<<i<<"]["<<j<<"]";

    cin>>A[i][j];

   }

  }

  cout<<"\n enter the elements of second matrix";

  for(i=0;i<p;i++)

  {

   for(j=0;j<q;j++)

   {

    cout<<"\n enter B["<<i<<"]["<<j<<"]";

    cin>>B[i][j];

   }

  }

  cout<<"\n the first matrix is :";

  for(i=0;i<m;i++)

  {

   cout<<"\n";

   for(j=0;j<n;j++)

   cout<<A[i][j]<<" ";

  }

  cout<<"\n second matrix is:";

  for(i=0;i<p;i++)

  {

   cout<<"\n ";

   for(j=0;j<q;j++)

   cout<<B[i][j]<<" ";

  }

  for(i=0;i<m;i++)

  {

   for(j=0;j<q;j++)

    {

        C[i][j]=0;

     }

for(i=0;i<m;i++)

  for(j=0;j<q;j++)

      for(k=0;k<n;k++)

   {

    C[i][j]+=A[i][k]*B[k][j];

   } 

  }

  cout<<"\n the product of two matrix is :";

  for(i=0;i<m;i++)

  {

   cout<<"\n";

   for(j=0;j<q;j++)

   cout<<C[i][j]<<" ";

  }

 }

 else

 cout<<"\n multiplication is not possible:";

 getch();

}

You Might Also Like

0 Comments

Like us on Facebook

Flickr Images

Flickr Short Films