Advertisement
dirtydevil123

2d diagonal sum

Jul 3rd, 2021
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int arr[4][4], row ,col,r_d=0,l_d=0;
  10.     for(col=0;col<4; col++){
  11.  
  12.         for (row=0;row<4;row++){
  13.             cout<<"enter the "<<col<<" x "<<row<<" enter :";
  14.             cin>> arr[col][row];
  15.  
  16.         }
  17.     }
  18.     cout<<"the 4x4 matrix enter by the user :"<< endl;
  19.  
  20.  
  21.     for(col=0;col<4; col++){
  22.         cout<<endl;
  23.         for (row=0;row<4;row++){
  24.             cout<<setw(4)<<arr[col][row];
  25.  
  26.         }
  27.     }
  28.     cout<<endl;
  29.  
  30.     for(col=0;col<4; col++){
  31.         cout<<endl;
  32.         for (row=0;row<4;row++){
  33.            if(col==row){
  34.              r_d=r_d + arr[col][row];
  35.             cout<<arr[col][row]<<" ";
  36.            }
  37.  
  38.         }
  39.     }
  40.     cout<<endl;
  41.     cout<<" right diagonal : "<<r_d;
  42.  
  43.     cout<<endl;
  44. int s=3;
  45.     for(col=0;col<4; col++){
  46.         cout<<endl;
  47.         for (row=0;row<4;row++){
  48.            if(row==s){
  49.              l_d=l_d + arr[col][row];
  50.             cout<<arr[col][row]<<" ";
  51.            }
  52.         }
  53.         s--;
  54.     }
  55.  
  56.     cout<<endl;
  57.     cout<<" left diagonal : "<<l_d;
  58.      cout<<endl;
  59.     cout<<"sum of diagonals : "<<r_d+l_d;
  60.  
  61.     return 0;
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement