Addition of Two Matrices using C++ Language

Program code:

#include<iostream>
using namespace std;

int main()
{
int a[2][2],b[2][2] , c[2][2];
cout<<"*********************************"<<endl;
cout<<"\tAddition of two Matrices"<<endl;
cout<<"*********************************"<<endl<<endl;
cout<<"Enter Elements of 2*2 Matrix A :"<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter Elements of Matrix B:"<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cin>>b[i][j];
}
}

cout<<endl<<"Matrix A =  "<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cout<<" \t\t "<<a[i][j]<<"  ";
}
cout<<endl;
}
cout<<endl<<"Matrix B =  "<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cout<<" \t\t "<<b[i][j]<<"  ";
}
cout<<endl;
}
for(int i =0 ; i< 2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
cout<<endl<<"Addition of two matrices is  =  "<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cout<<" \t\t "<<c[i][j]<<"  ";
}
cout<<endl;
}
return 0;
}

Output:


No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.