Program to find the sum of two matrices | C++ Programming
Tuesday, August 11, 2020
//Program to find the sum 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,j;
cout<<"\n enter the no of rows for A:";
cin>>m;
cout<<"\n enter the no of columns for A:";
cin>>n;
cout<<"\n enter the no of rows for B:";
cin>>p;
cout<<"\n enter the no of columns for B:";
cin>>q;
if(m==p && n==q)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{cout<<"\n enter A["<<i<<"]["<<j<<"] elements:";
cin>>A[i][j];
}
}
cout<<"\n the matrix A is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<A[i][j]<<" ";
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<"\n enter B["<<i<<"]["<<j<<"] elements:";
cin>>B[i][j];
}
}
cout<<"\n the matrix B 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<n;j++)
C[i][j]=A[i][j]+B[i][j];
}
cout<<"\n the sum of the two is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<C[i][j]<<" ";
}
}
else
{
cout<<"\n the two are not equal and hence not be added:";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int A[10][10],B[10][10],C[10][10],m,n,p,q,i,j;
cout<<"\n enter the no of rows for A:";
cin>>m;
cout<<"\n enter the no of columns for A:";
cin>>n;
cout<<"\n enter the no of rows for B:";
cin>>p;
cout<<"\n enter the no of columns for B:";
cin>>q;
if(m==p && n==q)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{cout<<"\n enter A["<<i<<"]["<<j<<"] elements:";
cin>>A[i][j];
}
}
cout<<"\n the matrix A is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<A[i][j]<<" ";
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<"\n enter B["<<i<<"]["<<j<<"] elements:";
cin>>B[i][j];
}
}
cout<<"\n the matrix B 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<n;j++)
C[i][j]=A[i][j]+B[i][j];
}
cout<<"\n the sum of the two is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<C[i][j]<<" ";
}
}
else
{
cout<<"\n the two are not equal and hence not be added:";
}
getch();
}
0 Comments