forked from gouravthakur39/beginners-C-program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diagonal-Difference.c
67 lines (58 loc) · 1.18 KB
/
Diagonal-Difference.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr.
scanf("%d", &n);
int arr[n][n];
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d ", &arr[i][j]);
//Taking diagonal sum of the matrix arr from the both side
if(arr[i][j]>=-100 && arr[i][j]<=100)
{
if(i==j)
{
sum1+=arr[i][j];
}
if(j==(n-1-i))
{
sum2+=arr[i][j];
}
}
}
}
// This code part belongs to the absolute difference between the sums of the matrix's along two diagonals
if((sum1-sum2)<0)
{
printf("%d", (-((sum1)-(sum2))));
}
else
{
printf("%d", ((sum1)-(sum2)));
}
return 0;
}
/*
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
*/