C++ Online Compiler
Example: Diagonal Difference C++ Program
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Diagonal Difference C++ Program #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; while (i<s) { int j=0; while (j<s) { cin >> arr[i][j]; j++; } i++; } // It will calculate the diagonal difference int df = 0, s2=s-1, d1=0, d2=0; i=0; while (i<s) { d1 += arr[i][i]; d2 += arr[i][s2]; s2--; i++; } 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