HackerRank C- Diagonal Difference




Complete the diagonalDifference function in the editor below. It must return an integer representing the absolute diagonal difference.

diagonalDifference takes the following parameter:

arr: an array of integers .



  1. #include<stdio.h>
  2. main()
  3. {
  4. int i,j,sum=0,sum1=0,sum2=0,n;
  5. scanf("%d",&n);
  6. int a[n][n];
  7. for(i=0;i<n;i++)
  8. {
  9. for(j=0;j<n;j++)
  10. { scanf("%d",&a[i][j]);
  11. }
  12. }
  13. i=0;
  14. for(j=0;i<n&&j<n;j++)
  15. {
  16. sum1=sum1+a[i][j];
  17. i++;
  18. }
  19. i=n-1;
  20. for(j=0;i>=0&&j<n;j++)
  21. {
  22. sum2=sum2+a[i][j];
  23. i--;
  24. }
  25. sum=sum1-sum2;
  26. if(sum<0)
  27. {
  28. sum=sum*-1;
  29. }
  30. printf("%d",sum);
  31. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube