Java Program to Compute the Sum of Diagonals of a Matrix

Last Updated : 21 Aug, 2026

Given a square matrix of size N × N, the task is to find the sum of its principal diagonal and secondary diagonal.

  • The principal diagonal runs from the top-left corner to the bottom-right corner.
  • The secondary diagonal runs from the top-right corner to the bottom-left corner.

Example:

Input 1:   6 7 3 4
               8 9 2 1
              1 2 9 6
              6 5 7 2
Output 1: Principal Diagonal: 26
                 Secondary Diagonal: 14

Input 2: 2 2 2
              1 1 1
             3 3 3
Output 2:  Principal Diagonal: 6
                  Secondary Diagonal: 6

Understanding Diagonals in a Square Matrix:

Consider a 4 × 4 matrix:

m00 m01 m02 m03
m10 m11 m12 m13
m20 m21 m22 m23
m30 m31 m32 m33

Principal Diagonal: The principal diagonal contains elements where the row index and column index are equal.

m00, m11, m22, m33 , row == column

Secondary Diagonal: The secondary diagonal contains elements where the sum of the row and column indices is N - 1.

m03, m12, m21, m30 , row + column == N - 1

1. Naive Approach: Using Nested Loops

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

Approach

  • Use two nested loops to visit every matrix element.
  • If i == j, add the element to the principal diagonal sum.
  • If i + j == N - 1, add the element to the secondary diagonal sum.
  • Print both sums.
Java
public class Geeks {

    static void sumOfDiagonals(int[][] matrix, int n) {

        int principalSum = 0;
        int secondarySum = 0;

        // Traverse the entire matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {

                // Principal diagonal
                if (i == j) {
                    principalSum += matrix[i][j];
                }

                // Secondary diagonal
                if (i + j == n - 1) {
                    secondarySum += matrix[i][j];
                }
            }
        }

        System.out.println("Principal Diagonal: " + principalSum);
        System.out.println("Secondary Diagonal: " + secondarySum);
    }

    public static void main(String[] args) {

        int[][] matrix = {
            {8, 2, 13, 4},
            {9, 16, 17, 8},
            {1, 22, 3, 14},
            {15, 6, 17, 8}
        };

        sumOfDiagonals(matrix, 4);
    }
}

Output
Principal Diagonal: 35
Secondary Diagonal: 58

Explanation

  • The outer loop selects each row.
  • The inner loop selects each column.
  • i == j identifies principal diagonal elements.
  • i + j == n - 1 identifies secondary diagonal elements.
  • Since every element of the matrix is checked, the algorithm takes O(N²) time.

2. Optimized Approach: Directly Access Diagonal Elements

Instead of traversing the complete matrix, we can directly access the diagonal elements. For every row i:

  • Principal diagonal element is matrix[i][i].
  • Secondary diagonal element is matrix[i][N - 1 - i].
Java
public class Geeks {

    static void sumOfDiagonals(int[][] matrix, int n) {

        int principalSum = 0;
        int secondarySum = 0;

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

            // Principal diagonal
            principalSum += matrix[i][i];

            // Secondary diagonal
            secondarySum += matrix[i][n - 1 - i];
        }

        System.out.println("Principal Diagonal: " + principalSum);
        System.out.println("Secondary Diagonal: " + secondarySum);
    }

    public static void main(String[] args) {

        int[][] matrix = {
            {8, 2, 13, 4},
            {9, 16, 17, 8},
            {1, 22, 3, 14},
            {15, 6, 17, 8}
        };

        sumOfDiagonals(matrix, 4);
    }
}

Output
Principal Diagonal: 35
Secondary Diagonal: 58

Explanation

  • matrix[i][i] directly accesses the principal diagonal element.
  • matrix[i][n - 1 - i] directly accesses the secondary diagonal element.
  • Only one loop is required because each iteration processes one element from each diagonal.
  • This avoids checking the remaining elements of the matrix.
Comment