Diagonal Difference | Hacker Rank Solution in C
Problem:
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = . The right to left diagonal = . Their absolute difference is .
Solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a[100][100],n,c=0,d=0,i,j,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
c=c+a[i][i];
for(i=0;i<n;i++)
d=d+a[i][n-1-i];
sum=abs(c-d);
printf("%d",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Comments
Post a Comment