RECENTLY PUBLISHED

6/recent/ticker-posts

C++ Program to add two matrix.

 ADDITION OF TWO MATRIX



This is simple C++ program to add two matrices .

Before adding two matrices we have to know that if order of both matrices is not same then addition of matrices can not be performed.

We have done program that will check whether addition of two matrices can perform or not .

If  we will receive answer 'yes' then program will calculates addition of those matrices and will print that matrix on screen.


C++ Program 



#include<iostream>

using namespace std;

int main()

{

 int a[50][50],b[50][50],c[50][50];
   
 int i,j,m,n,k,l;

    cout<<"Enter rows of matrix a:";
   
 cin>>m;
  
  cout<<"Enter columns of matrix a:";

    cin>>n;
  
  cout<<"Enter rows of matrix b:";
    
cin>>k;
    
cout<<"Enter columns of matrix b:";
   
 cin>>l;
  
   if((m != k) && (n != l))
    
{

     cout<<"**Sorry,Matrix addition is not possible here,"<<endl<<"Because order of Matrices are different.**";

     exit(0);
  
  }
 
   cout<<"**Matrix addition is possible**"<<endl;
    
cout<<"Enter elements of matrix a:"<<endl;
   
 for(i=0;i<m;i++)
    
{
    
  for(j=0;j<n;j++)
    
 {
    
   cin>>a[i][j];
   
  }

    }

    cout<<"Enter elements of matrix b:"<<endl;
   
  for(i=0;i<k;i++)
    
{

        for(j=0;j<l;j++)
  
      {
   
         cin>>b[i][j];
 
       }
 
   }
 
   for(i=0;i<m;i++)
    
{


     for(j=0;j<n;j++)
    
   {
      
  c[i][j]=a[i][j]+b[i][j];

      
  }
   
    }
  
  cout<<"Addition of Matrix is:"<<endl;

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

     for(j=0;j<n;j++)
    
 {
    
  cout<<c[i][j]<<"   ";

    }
   
  cout<<endl;
    
}
 
    return 0;

}



 INPUT && OUTPUT









Post a Comment

0 Comments