C++ Program for Addition, Subtraction and Division of Two Matrix

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Arithmatic operation of Two Matrix Add Sub Division
#include<iostream>
#define clrscr() system("cls")
using namespace std;

int main()
{
   int a[50][50],b[50][50],result[50][50],r,c,m,n;
   clrscr();
   xyz:cout<<"\nEnter  value of row and column for matrix\n";
   cin>>m>>n;
   if(m>50 || n>50)
   {
      cout<<"Invalid values enter again";
      goto xyz;
   }
   else
   {
           // Read First Matrix
             cout<<"\nEnter elements in first matrix";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cin>>a[r][c];
                }
             }
             // Read Second Matrix
            cout<<"\nEnter elements in second matrix";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cin>>b[r][c];
                }
             }  
         // Print First Matrix 
          cout<<"\nElements of first matrix\n";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cout<<a[r][c]<<"\t";
                }
                cout<<"\n";
             }
      // Print Second Matrix 
          cout<<"\nElements of second matrix\n";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cout<<b[r][c]<<"\t";
                }
                cout<<"\n";
             }
           // Matrix Addition
          for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    result[r][c]=a[r][c]+b[r][c];
                }
                cout<<"\n";
             }   
              cout<<"\n Addition of two matrix\n";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cout<<result[r][c]<<"\t";
                }
                cout<<"\n";
             }

           // Matrix Subtraction
          for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    result[r][c]=a[r][c]-b[r][c];
                }
                cout<<"\n";
             }   

            cout<<"\n Subtraction of two matrix\n";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cout<<result[r][c]<<"\t";
                }
                cout<<"\n";
             }

           // Matrix division
          for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    result[r][c]=a[r][c]/b[r][c];
                }
                cout<<"\n";
             }   

            cout<<"\n Division of two matrix\n";
             for(r=0;r<m;r++)
             {
                for(c=0;c<n;c++)
                {
                    cout<<result[r][c]<<"\t";
                }
                cout<<"\n";
             }



   }

}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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