-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagonal-diff.c
48 lines (39 loc) · 961 Bytes
/
diagonal-diff.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
/* Given a 3x3 Matrix input from the user, calculate the absolute difference between the sums of its diagonals */
#include <stdio.h>
#include <stdlib.h>
void main()
{
int arr[3][3];
int i,j;
int sum1=0;
int sum2=0;
printf("Enter the 3x3 Matrix: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("The Matrix is:\n");
for(i=0; i<3; i++)
{
for (j = 0; j < 3; j ++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
for(i=0; i<3; i++)
{
for (j = 0; j < 3; j ++)
{
if (i==j)
sum1 = sum1 + arr[i][j];
}
sum2 = sum2 + arr[i][2-i];
}
printf("The sum of the left to right diagonal = %d\n", sum1);
printf("The sum of the right ti left diagonal = %d\n", sum2);
printf("The absolute difference = %d\n ", abs(sum1-sum2));
}