C++ Program to Efficiently Compute Sums of Diagonals of a Matrix

Last Updated : 20 Aug, 2026

Given a square matrix, the task is to find the sum of its principal diagonal and secondary diagonal. The principal diagonal runs from the top-left to the bottom-right, while the secondary diagonal runs from the top-right to the bottom-left. 

  • Principal diagonal elements satisfy row = column.
  • Secondary diagonal elements satisfy row + column = n - 1.

Examples

Input:

4
1 2 3 4
4 3 2 1
7 8 9 6
6 5 4 3

Output:

Principal Diagonal: 16
Secondary Diagonal: 20

Input:

3
1 1 1
1 1 1
1 1 1

Output:

Principal Diagonal: 3
Secondary Diagonal: 3

Approaches to Find the Sum of Matrix Diagonals

There are two approaches to find the sum of the principal and secondary diagonals:

Approach 1: Using Nested Loops

The first approach traverses every element of the matrix and checks whether it belongs to the principal or secondary diagonal.

  • For the principal diagonal, the condition is i == j.
  • For the secondary diagonal, the condition is i + j == n - 1.
  • Add the corresponding elements to their respective sums.
C++
#include <iostream>
using namespace std;

void diagonalSums(int mat[][100], int n) {
    int principal = 0, secondary = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {

            // Principal diagonal
            if (i == j)
                principal += mat[i][j];

            // Secondary diagonal
            if (i + j == n - 1)
                secondary += mat[i][j];
        }
    }

    cout << "Principal Diagonal: " << principal << endl;
    cout << "Secondary Diagonal: " << secondary << endl;
}

int main() {
    int mat[100][100] = {
        {1, 2, 3, 4},
        {4, 3, 2, 1},
        {7, 8, 9, 6},
        {6, 5, 4, 3}
    };

    diagonalSums(mat, 4);

    return 0;
} 

Output
Principal Diagonal: 16
Secondary Diagonal: 20

Explanation

  • The outer and inner loops traverse every element of the matrix.
  • i == j identifies elements of the principal diagonal, while i + j == n - 1 identifies elements of the secondary diagonal.
  • The corresponding elements are added to principal and secondary, which are printed at the end.

Approach 2: Efficient One-Loop Approach

Instead of checking every element, we can directly access the elements belonging to both diagonals.

  • The principal diagonal element at row i is mat[i][i].
  • The secondary diagonal element at row i is mat[i][n - i - 1].
  • Traverse the matrix rows once and add both elements.
C++
#include <iostream>
using namespace std;

void diagonalSums(int mat[][100], int n) {
    int principal = 0, secondary = 0;

    for (int i = 0; i < n; i++) {
        principal += mat[i][i];
        secondary += mat[i][n - i - 1];
    }

    cout << "Principal Diagonal: " << principal << endl;
    cout << "Secondary Diagonal: " << secondary << endl;
}

int main() {
    int mat[100][100] = {
        {1, 2, 3, 4},
        {4, 3, 2, 1},
        {7, 8, 9, 6},
        {6, 5, 4, 3}
    };

    diagonalSums(mat, 4);

    return 0;
} 

Output
Principal Diagonal: 16
Secondary Diagonal: 20

Explanation

  • A single loop directly accesses the diagonal elements instead of checking every matrix element.
  • mat[i][i] gives the principal diagonal element, while mat[i][n - i - 1] gives the secondary diagonal element.
  • Both sums are calculated in one traversal and printed at the end.
Comment