C++ Program to Add Two Matrix
Write C++ Program to Add Two Matrix
// CPP Program to Add Two Matrix #include <iostream> using namespace std; int main() { int A[3][3] = { {1,2,3}, {3,4,6}, {1,2,3} }; int B[3][3] = { {4,5,6}, {1,2,3}, {1,4,9} }; int C[3][3], i, j; cout << "Matrix A :--> \n"; for(i = 0;i < 3; i++) { for( j = 0; j < 3; j++) { cout << A[i][j] << " "; } cout << endl; } cout << "\n\nMatrix B :--> \n"; for(i = 0;i < 3; i++) { for( j = 0; j < 3; j++) { cout << B[i][j] << " "; } cout << endl; } cout << "\n\nAddition of Matrix A and B:--> \n"; for(i = 0;i < 3; i++) { for( j = 0; j < 3; j++) { C[i][j] = A[i][j] + B[i][j]; cout << C[i][j] << " "; } cout << endl; } return 0; }
Output:
Matrix A :-->
1 2 3
3 4 6
1 2 3
Matrix B :-->
4 5 6
1 2 3
1 4 9
Addition of Matrix A and B:-->
5 7 9
4 6 9
2 6 12