# C++ Program to Add Two Matrices

In this article, you will learn and get code on addition of two matrices in C++. There are two programs available here on matrix addition:

• Addition of Two 3*3 Matrix
• Addition of Two Matrix with its Size defined by User at run-time

In both the cases, the elements of matrix must be entered by user at run-time.

### How Two Matrix can be Added ?

The separate article is created for matrix addition. If you're not aware about it, then I recommend you to follow the article for complete understanding about it in very short time.

But for now, let me tell you about it, i.e., to add two matrices, we have to add corresponding elements. That is, element at first row and first column of first matrix gets added with the element at first row and first column of second matrix. In similar way, the element at first row and second column of first matrix gets added with the element at first row and second column of second matrix. And so on.

To add two matrices in C++ programming, you have to ask from user to enter the elements of both the matrix. Now add the same positioned elements to form a new matrix. After adding two matrices, display the third matrix which is the addition result of two matrices as shown in the following program.

## Addition of Two 3*3 Matrix

This program asks user to enter any 3*3 two matrices. That is, user is allowed to enter 9 elements for first and 9 elements for second matrix. And then add them up to print the third matrix that will be the addition result of given two:

```#include<iostream>
using namespace std;
int main()
{
int mat1, mat2, i, j, mat3;
cout<<"Enter Elements of First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>mat1[i][j];
}
cout<<"Enter Elements of Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>mat2[i][j];
}
cout<<"\nAdding the Two Given Matrix...\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
mat3[i][j] = mat1[i][j]+mat2[i][j];
}
cout<<"Addition Result of Two Given Matrix is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<mat3[i][j]<<" ";
cout<<endl;
}
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now enter any 9 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9 as the elements of first matrix and then again 9 numbers say 9, 8, 7, 6, 5, 4, 3, 2, 1 as elements of second matrix. Press `ENTER` key to see the addition result of two given matrix as shown in the output given below:

When user enters 9 elements of first matrix, then it gets stored in a way that:

• First element gets stored at mat1
• Second at mat1
• Third at mat1
• Fourth at mat1
• Fifth at mat1
• and so on

Similarly, 9 elements of second matrix also gets stored in similar way. And the addition of two matrix goes in a way that:

• mat3 = mat1+mat2. That is, the first element of first matrix gets added with first element of second matrix and initialized as the first element of third matrix
• mat3 = mat1+mat2. That is, second element of both matrix gets added and initialized as second matrix of third matrix
• In similar way, the matrix gets added

Print the third matrix, that will be the summation result of two given matrix.

### Allow User to Define Size

This program allows user to define the size of both the matrix.

Note - To define addition of two matrix, both the matrix must be of same order. Here same order means, row size of first matrix must be equal to the row size of second matrix and column size of first matrix must also be equal to the column size of second matrix.

```#include<iostream>
using namespace std;
int main()
{
int rowOne, colOne, rowTwo, colTwo, i, j;
int mat1, mat2, sum=0;
cout<<"Enter Row and Column Size of First Matrix: ";
cin>>rowOne>>colOne;
cout<<"Enter Row and Column Size of Second Matrix: ";
cin>>rowTwo>>colTwo;
if(rowOne==rowTwo && colOne==colTwo)
{
cout<<"\nEnter "<<rowOne<<"*"<<colOne<<" First Matrix Elements: ";
for(i=0; i<rowOne; i++)
{
for(j=0; j<colOne; j++)
cin>>mat1[i][j];
}
cout<<"Enter "<<rowOne<<"*"<<colOne<<" Second Matrix Elements: ";
for(i=0; i<rowOne; i++)
{
for(j=0; j<colOne; j++)
cin>>mat2[i][j];
}
cout<<"\nAddition Result of Two Given Matrix is:\n";
for(i=0; i<rowOne; i++)
{
for(j=0; j<colOne; j++)
{
sum = mat1[i][j]+mat2[i][j];
cout<<sum<<"  ";
}
cout<<endl;
}
}
else
cout<<"\nSize Mismatched!\n";
return 0;
}```

Here is its sample run supposing that user has enters the row size and column size for both the matrix is 2 and 3, and its elements for first matrix is 1, 2, 3, 4, 5, 6 and for second matrix is 6, 5, 4, 3, 2, 1:

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋