TCS-NQT-C program-Absolute difference of sum of diagonal elements of a Matrix
abs_diff of sum of diagonal elements of matrix
C - program:
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int n,j;
int i=0,rds=0,lds=0,f,s;
scanf("%d",&n);
int a[n][n];
for(int f=0;f<n;f++)
{
for(s=0;s<n;s++)
{
scanf("%d",&a[f][s]);
}
}
while(i<n)
{
rds=rds+a[i][i];
i++;
}
j=n-1,i=0;
while(i<n)
{
lds=lds+a[i][j];
i++;
j--;
}
printf("%d",abs(rds-lds));
return 0;
}
Input : a[][] = 10 2
4 5
Output : 7
Input : a[][] = 11 2 4
4 5 6
10 8 -12
Output : 15
Explanation Sum of primary diagonal = 11 + 5 + (-12) = 4. Sum of primary diagonal = 4 + 5 + 10 = 19. Difference = |19 - 4| = 15.
Comments
Post a Comment