- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see how to get the difference between the sums of two diagonals of a given matrix. Suppose we have a matrix of order N x N, we have to get the sum of primary and secondary diagonals, then get the difference of them. To get the major diagonal, we know that the row index and column index increases simultaneously. For the second diagonal, row index and column index values are increased by this formula row_index = n – 1 – col_index. After getting the sum, take the difference and return a result.

#include<iostream> #include<cmath> #define MAX 100 using namespace std; int diagonalSumDifference(int matrix[][MAX], int n) { int sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) { sum1 += matrix[i][i]; sum2 += matrix[i][n-i-1]; } return abs(sum1 - sum2); } // Driven Program int main() { int n = 3; int matrix[][MAX] = { {11, 2, 4}, {4 , 5, 6}, {10, 8, -12} }; cout << "Difference of the sum of two diagonals: " << diagonalSumDifference(matrix, n); }

Difference of the sum of two diagonals: 15

- Related Questions & Answers
- Difference between sums of odd and even digits.
- C Program for Difference between sums of odd and even digits?
- Find minimum difference between any two element in C++
- Find the compatibility difference between two arrays in C++
- C Program for the Difference between sums of odd and even digits?
- Find number of diagonals in n sided convex polygon in C++
- Program to find maximum sum of two sets where sums are equal in C++
- Possible two sets from first N natural numbers difference of sums as D in C++
- C# difference in milliseconds between two DateTime
- Python Program for Difference between sums of odd and even digits
- Find the Symmetric difference between two arrays - JavaScript
- Python program to find difference between two timestamps
- Maximum absolute difference of value and index sums in C
- Maximum difference between two subsets of m elements in C
- Find the minimum difference between Shifted tables of two numbers in Python

Advertisements