# C++ program for addition of two matrices

In this article, we are going to learn how to add two matrices in the C++ program. We will take the input from the user for both the matrices and then we will perform the add operation on both the matrices.

### 1. How to Addition of Two Matrix of Fix size

In this example, we are going to add two matrices of size 3×3 means 3 rows and 3 column matrices. We will ask the user to enter the values for each index, and once we have the matrices, then we will add them index by index.Matrix addition performs the element of one matrix at index [n][m] to the elements of another matrix at index [n][m].This means the  index of matrix one gets added to  of matrix two and so on.

After the addition is completed then the result will be a matrix of the same size as matrix one and matrix two. let us understand this with the help of the below code example.

### C++ Program for Addition of Two Matrix of Fix size

```#include<iostream>
using namespace std;
int main()
{
int Matrix1;
int Matrix2;
int ResultMatrix;
int row, col;

cout<<"Please enter the Elements of First Matrix: ";
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
cin>> Matrix1[row][col];
}

cout<<"Please enter the Elements Second Matrix: ";
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
cin>>Matrix2[row][col];
}

for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
ResultMatrix[row][col] = Matrix1[row][col] + Matrix2[row][col];
}

cout<<"The Resulted Matrix after addition is:\n";
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
cout<< ResultMatrix[row][col]<<" ";
cout<<endl;
}
return 0;
}
```

Output

```Output:
Please enter the Elements of First Matrix: 1
2
3
4
5
6
7
8
9
Please enter the Elements Second Matrix: 9
8
7
6
5
4
3
2
1
The Resulted Matrix after addition is:
10 10 10
10 10 10
10 10 10

```

### How to Add Two Matrix entered by the user

In the above program, we have a limitation that size is fixed. But in this example, we will take the size of the matrix from the user. We will need to make sure the size is the same for both matrices provided by the user, which means the number of rows in the first matrix must be the same as in the second matrix. Similarly, the number of columns in the first matrix must be the same in the second matrix. So let us understand this with the help of the below example.

### C++ program for the addition of two matrices

```#include<iostream>
using namespace std;
int main()
{

int Matrix1;
int Matrix2;
int ResultMatrix;
int RowSize , ColumnSize;
int row, col;

cout<<"Enter Row and Column Size of Matrix [Max size 10x10]: ";
cin>>RowSize>>ColumnSize;

cout<<"Please enter the Elements of First Matrix: ";
for(row=0; row< RowSize; row++)
{
for(col=0; col< ColumnSize; col++)
cin>> Matrix1[row][col];
}

cout<<"Please enter the Elements Second Matrix: ";
for(row=0; row< RowSize; row++)
{
for(col=0; col< ColumnSize; col++)
cin>>Matrix2[row][col];
}

for(row=0; row< RowSize; row++)
{
for(col=0; col< ColumnSize; col++)
ResultMatrix[row][col] = Matrix1[row][col] + Matrix2[row][col];
}

cout<<"The Resulted Matrix after addition is:\n";
for(row=0; row< RowSize; row++)
{
for(col=0; col< ColumnSize; col++)
cout<< ResultMatrix[row][col]<<" ";
cout<<endl;
}
return 0;
}
```

Output

```Enter Row and Column Size of Matrix [Max size 10x10]: 3
3
Please enter the Elements of First Matrix: 1
2
3
4
5
6
7
8
9
Please enter the Elements Second Matrix: 9
8
7
6
5
4
3
2
1
The Resulted Matrix after addition is:
10 10 10
10 10 10
10 10 10

```