# Add Two Matrix

“** Add Two Matrix**” is a basic operation which can be applied over two or more matrices. Here, we have given two matrices of dimension N*M and our task is to add the following matrices and store the result in resultant matrix.

CONDITION: The dimensions of the matrices to be added must be same. Otherwise, we cannot add two matrices.

**Example:**

Number of rows = 3

Number of columns = 3

1 | 1 | 1 |

2 | 2 | 2 |

3 | 3 | 3 |

4 | 4 | 4 |

5 | 5 | 5 |

6 | 6 | 6 |

5 | 5 | 5 |

7 | 7 | 7 |

9 | 9 | 9 |

**C++ Program to add two matrices is as follows:**

#include<bits/stdc++.h> using namespace std; int main() { int N,M; // Scan Dimensions of the matrix cout<<"Enter Row Size and Column Size of Matrix:\n"; cin>>N>>M; int matrix1[N][M]; int matrix2[N][M]; int result[N][M]; // Scan Matrix1 Elements cout<<"Enter the Elements of First Matrix:\n"; for(int i = 0 ; i < N ; i++) for(int j = 0 ; j < M ; j++) cin>>matrix1[i][j]; // Scan Matrix2 Elements cout<<"Enter the Elements of Second Matrix:\n"; for(int i = 0 ; i < N ; i++) for(int j = 0 ; j < M ; j++) cin>>matrix2[i][j]; // Adding matrices for(int i = 0 ; i < N ; i++) for(int j = 0 ; j < M ; j++) result[i][j] = matrix1[i][j]+matrix2[i][j]; //Print Resultant matrix cout<<"The Resultant matrix is:\n"; for(int i = 0 ; i < N ; i++) { for(int j = 0 ; j < M ; j++) { cout<<result[i][j]<<" "; } cout<<endl; } }

Enter Row Size and Column Size of Matrix: 3 3 Enter the Elements of First Matrix: 1 2 3 1 2 3 1 2 3 Enter the Elements of Second Matrix: 1 2 3 1 2 3 1 2 3 The Resultant matrix is: 2 4 6 2 4 6 2 4 6OUTPUT: