## Question Reference

C++ Home

### C++ Inbuilt Functions

C++ String Functions

### C++ Interview Programs

C++ Common ProgramsC++ Array ProgramsC++ Converter ProgramsC++ Star Patterns ProgramC++ Number Patterns ProgramC++ Alphabet Patterns Program

ASCII Table

# C++ Program to perform Matrix Addition

Last Updated:

In the following example, we will add the two given matrices (two-dimensional arrays).

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, arr3[3][3]; int arr1[3][3] = { {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; int arr2[3][3] = { {2, 2, 2}, {2, 2, 2}, {2, 2, 2} }; cout << "Matrix A (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr1[i][j] << " "; cout << "\n"; } cout << "\nMatrix B (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr2[i][j] << " "; cout << "\n"; } cout << "\nMatrix Addition (A + B): \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j] = arr1[i][j] + arr2[i][j]; cout << arr3[i][j] << " "; } cout << "\n"; } return 0; }

In the following example, we will get the values for (3 x 3) Matrices A and B from the user and display the matrix addition.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, arr3[3][3], arr2[3][3], arr1[3][3]; cout << "Enter Matrix A (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cin >> arr1[i][j]; } cout << "\nEnter Matrix B (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cin >> arr2[i][j]; } cout << "\nMatrix Addition (A + B): \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j] = arr1[i][j] + arr2[i][j]; cout << arr3[i][j] << " "; } cout << "\n"; } return 0; }