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

Addition and subtraction of two matrix

These two programs illustrate the concept addition and subtraction of two matrix.

  • Here in this program “i” is number of rows and “j” is number of coloumns.
  • “a[3][3]” is the name of first array with 3 rows and 3 coloumns .
  • “b[3][3]” is the name of second array with 3 rows and 3 coloumns .
  • “c[3][3]” is the name of resultant array with 3 rows and 3 coloumns .In this array result will be stored.

Addition of two matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[3][3],b[3][3],c[3][3],i,j;
 cout<<"Enter First Matrix row wise:-\n";

 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     cin>>a[i][j];
    }
  }

  cout<<"Enter Second Matrix row wise:-\n";

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



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

  cout<<"Sum of two  Matrix is:-\n";
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     cout<<"\t"<<c[i][j];

    }
    cout<<"\n";
  }
 getch();
}

OUTPUT

addition and subtraction of two matrix


Subtraction of two matrix

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[3][3],b[3][3],c[3][3],i,j;
 cout<<"Enter First Matrix row wise:-\n";

 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     cin>>a[i][j];
    }
  }

  cout<<"Enter Second Matrix row wise:-\n";

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



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

  cout<<"Subtraction of two  Matrix is:-\n";
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     cout<<"\t"<<c[i][j];

    }
    cout<<"\n";
  }
 getch();
}

addition and subtraction of two matrix


Comment below if you are facing any problem


Also read

C++ simple program to enter 3 x 3 matrix
C program to multiply two matrix
How to add two Matrices in c program

Leave a Reply

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