C++ Online Compiler
Example: Diagonal Difference C++ Program using For loop
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Diagonal Difference C++ Program using For loop #include <iostream> using namespace std; int main() { int s = 0; cout << "Enter the size of the array::\n"; cin >> s; int arr[s][s]; cout << "\nEnter the elements of the array in [" << s << " x " << s << "] matrix form::\n"; int i=0; for (; i<s; i++) { int j=0; for (; j<s; j++) { cin >> 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 cout << "\nDiagonal Difference = " << df << "\n"; return 0; }
4 10 8 -12 2 4 5 90 3 11 2 4 6 4 5 6 7
Output
Clear
ADVERTISEMENTS