Absolute difference of the sum across the diagonalsD:424
You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.
Input Format:
The first line will contain the value of N.
The next N lines will contain the N values separated by one or more spaces.
Output Format:
The absolute difference of the sums across the two main diagonals.
Boundary Conditions:
2 <= N <= 20
Example Input/Output 1:
Input:
2
10 9
4 22
Output:
19
Explanation:
The sum along the first diagonal is 10+22 = 32
The sum along the first diagonal is 4+9=13
The absolute difference is 32-13= 19
Example Input/Output 2:
Input:
2
-10 6
4 -22
Output:
22
PROGRAM IN C:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n,sum=0,i,j,sum1=0,to=0;
scanf("%d",&n);
int a[100][100];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==n-j-1)
sum1=sum1+a[i][j];
}
}
if(sum<0)
{
sum=-sum;
}
if(sum1<0)
{
sum1=-sum1;
}
to=sum-sum1;
printf("%d",abs(to));
return 0;
}
Comments
Post a Comment