In this program, we are going to share how to add two matrices using C++ programming language. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C++ program to add two matrices.
Copy the below C program and execute it with the help of Turbo C compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include<iostream> using namespace std; main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; cout << "Enter the number of rows and columns of matrix "; cin >> m >> n; cout << "Enter the elements of first matrix\n"; for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) cin >> first[c][d]; cout << "Enter the elements of second matrix\n"; for ( c = 0 ; c < m ;c++ ) for ( d = 0 ; d < n ; d++ ) cin >> second[c][d]; for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; cout << "Sum of entered matrices:-\n"; for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) cout << sum[c][d] << "\t"; cout << endl; } return 0; } |
Enter the number of rows and columns of matrix 2
5
Enter the elements of first matrix
10
20
30
40
50
60
70
80
90
100
Enter the elements of second matrix
2
3
4
5
6
7
8
9
10
11
Sum of entered matrices:-
12 23 34 45 56
67 78 89 100 111
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: add two matrices in C++ Programming, adding the two matrix to form a new matrix, addition of the two matrices, C++ program to add two matrices, C++ Program to Add Two Matrix Using Multi Dimensional Arrays