C Online Compiler
Example: Diagonal Difference in C Program using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Diagonal Difference in C Program using For loop #include <stdio.h> int main() { int s = 0; printf("Enter the size of the array::\n"); scanf("%d", &s); int arr[s][s]; printf("\nEnter the elements of the array in [%d x %d] matrix form::\n", s, s); int i=0; for (; i<s; i++) { int j=0; for (; j<s; j++) { scanf("%d", &arr[i][j]); } } // It will calculate the diagonal difference int df = 0, s2=s-1, d1=0, d2=0; i=0; for (; i<s; i++) { d1 += arr[i][i]; d2 += arr[i][s2]; s2--; } df = d1 > d2 ? d1 - d2 : d2 - d1; // It will return the final output printf("\nDiagonal Difference = %d\n", df); return 0; }
4 11 2 4 6 4 5 6 7 10 8 -12 2 4 5 90 3
Output
Clear
ADVERTISEMENTS