Diagonal Difference

Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Explanation
The primary diagonal is:
11
      5
            -12
Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:
            4
      5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15


#include<iomanip.h>

int main(){
    int n;
    cin >> n;
   
    
   
    int   m[n][n],i,j,sum=0,sum2=0;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>m[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum+=m[i][j];
if(i+j==n-1)
sum2+=m[i][j];
}
}
cout<<abs(sum-sum2);

    return 0;
}

Comments

Popular posts from this blog

Pre-compiled Headers

CONNECTING WITH FTP-FILE TRANSFER PROTOCOL

PROBING THE INTERNET WITH TELNET: