Diagonal Difference Hacker Rank Solution In C-language | | Hacker Rank Solution

Diagonal Difference Hacker Rank Solution In C-language | | Hacker Rank Solution

Question


Given a square matrix, calculate the absolute difference between the sums of its diagonals.
Input Format

The first line contains a single integer,  denoting the number of rows and columns in the matrix 
The next  lines denote the matrix 's rows, with each line containing  space-separated integers describing the columns.

Answer


#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;
    int i=0,sum=0,j,sum2=0;
    scanf("%d",&n);
    int a[n][n];
    for(int a_i = 0; a_i < n; a_i++){
       for(int a_j = 0; a_j < n; a_j++){
         
          scanf("%d",&a[a_i][a_j]);
       }
    }
 while(i<n)
 {
  sum=sum+a[i][i];
  i++;
 }
    j=n-1,i=0;
 while(i<n)
 {
  sum2=sum2+a[i][j];
  i++;
        j--;
 }
    printf("%d",abs(sum-sum2));
    return 0;
}

Comments :

Post a Comment