Program for addition of two matrices

In this program we compute addition of two matrices A and B which has same order. First we ask user to  enter order or size of matrix, then enter numbers for matrix A and B. To understand the strategy, let us have  look at the dummy matrices. Suppose matrices have order 3×3.

Also Read: Program for multiplication of two matrices

Matrix A :

3     7     3

9     1     4

0     2     6

Matrix B :

8     3     5

4     2     0

7     5     8

Resultant Matrix is addition of matrices A and B :

11     10      8

13      3       4

7        7     14

 Also Read:  Program for subtraction of two matrices

#include<iostream.h>

#include<conio.h>

 

int main()

{

clrscr();

int a[10][10];

int b[10][10];

int x,y,i,j;

 

 

cout<<“\n Enter the number of rows and columns\n\n”;

cin>>x>>y;

 

cout<<” Enter elements for Matrix A \n\n”;

 

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>a[i][j];

}

cout<<“\n”;

}

 

cout<<” Enter elements for Matrix B \n\n”;

 

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>b[i][j];

}

cout<<“\n”;

}

 

 

cout<<“\n\nMatrix A :\n\n”;

 

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<a[i][j];

}

cout<<“\n\n”;

}

 

cout<<“\n\nMatrix B :\n\n”;

 

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<b[i][j];

}

cout<<“\n\n”;

}

 

 

cout<<“\n\nAddition of Matrix A and Matrix B :\n\n”;

 

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<a[i][j]+b[i][j];

}

cout<<“\n\n”;

}

 

getch();

return 0;

}

Mohit Arora
Follow me