You are here

C++ program to add two matrices

C++ program to add two matrices.

C++programming code

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. main()
  6. {
  7.    int m, n, c, d, first[10][10], second[10][10], sum[10][10];
  8.    
  9.    cout << "Enter the number of rows and columns of matrix ";
  10.    cin >> m >> n;
  11.    cout << "Enter the elements of first matrix\n";
  12.  
  13.    for (  c = 0 ; c < m ; c++ )
  14.       for ( d = 0 ; d < n ; d++ )
  15.          cin >> first[c][d];
  16.  
  17.    cout << "Enter the elements of second matrix\n";
  18.  
  19.    for ( c = 0 ; c < m ;c++ )
  20.       for ( d = 0 ; d < n ; d++ )
  21.             cin >> second[c][d];
  22.  
  23.    for ( c = 0 ; c < m ; c++ )
  24.       for ( d = 0 ; d < n ; d++ )
  25.          sum[c][d] = first[c][d] + second[c][d];
  26.  
  27.    cout << "Sum of entered matrices:-\n";
  28.  
  29.    for ( c = 0 ; c < m ; c++ )
  30.    {
  31.       for ( d = 0 ; d < n ; d++ )
  32.          cout << sum[c][d] << "\t";
  33.  
  34.       cout << endl;
  35.    }
  36.  
  37.    return 0;
  38. }