Introduction
Printing the diagonal elements of a matrix is one of the most fundamental matrix operations in Java. Every square matrix contains two important diagonals:
- Main diagonal (Primary diagonal) – runs from the top-left corner to the bottom-right corner.
- Anti-diagonal (Secondary diagonal) – runs from the top-right corner to the bottom-left corner.
Each diagonal follows a simple indexing formula. Understanding these formulas is essential because they are used in many matrix-related problems such as calculating diagonal sums, checking identity matrices, finding trace values, and solving pattern-based programming questions.
In this tutorial, you'll learn how to print both diagonals using Java, understand the indexing formulas, and avoid common mistakes.
Problem Statement
Given the following matrix:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
The expected output is:
Main Diagonal : 1 5 9
Anti Diagonal : 3 5 7
Main Diagonal (Primary Diagonal)
The main diagonal consists of elements where the row index is equal to the column index.
Formula:
matrix[i][i]
Java Program
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
System.out.print("Main Diagonal: ");
for (int i = 0; i < matrix.length; i++) {
System.out.print(matrix[i][i] + " ");
}
}
}
Output
Main Diagonal: 1 5 9
Anti-Diagonal (Secondary Diagonal)
The anti-diagonal consists of elements where:
column = n - 1 - row
Formula:
matrix[i][n - 1 - i]
where n is the size of the square matrix.
Java Program
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int n = matrix.length;
System.out.print("Anti Diagonal: ");
for (int i = 0; i < n; i++) {
System.out.print(matrix[i][n - 1 - i] + " ");
}
}
}
Output
Anti Diagonal: 3 5 7
Printing Both Diagonals Together
You can print both diagonals using two simple loops.
Java Program
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int n = matrix.length;
System.out.print("Main Diagonal: ");
for (int i = 0; i < n; i++) {
System.out.print(matrix[i][i] + " ");
}
System.out.println();
System.out.print("Anti Diagonal: ");
for (int i = 0; i < n; i++) {
System.out.print(matrix[i][n - 1 - i] + " ");
}
}
}
Output
Main Diagonal: 1 5 9
Anti Diagonal: 3 5 7
Step-by-Step Explanation
Main Diagonal
The formula is:
matrix[i][i]
As i increases:
matrix[0][0]
matrix[1][1]
matrix[2][2]
The traversal moves:
- One row down
- One column right
This traces the main diagonal.
Anti-Diagonal
The formula is:
matrix[i][n - 1 - i]
As i increases:
matrix[0][2]
matrix[1][1]
matrix[2][0]
The traversal moves:
- One row down
- One column left
This traces the anti-diagonal.
Internal Working
For the following matrix:
{
{1,2,3},
{4,5,6},
{7,8,9}
}
Main Diagonal
i = 0 → matrix[0][0] = 1
i = 1 → matrix[1][1] = 5
i = 2 → matrix[2][2] = 9
Result
1 5 9
Anti-Diagonal
i = 0 → matrix[0][2] = 3
i = 1 → matrix[1][1] = 5
i = 2 → matrix[2][0] = 7
Result
3 5 7
Notice that the middle element (5) belongs to both diagonals.
Real-Life Analogy
Imagine a chessboard.
If you draw a line from the top-left corner to the bottom-right corner, you get the main diagonal.
If you draw another line from the top-right corner to the bottom-left corner, you get the anti-diagonal.
For boards with an odd number of rows and columns, these two lines intersect exactly at the center square.
Best Practices
- Use
matrix[i][i]for the main diagonal. - Use
matrix[i][n - 1 - i]for the anti-diagonal. - Always derive
nusingmatrix.length. - Ensure the matrix is square before printing diagonals.
- Remember that the center element belongs to both diagonals in odd-sized matrices.
Common Mistakes
Mixing Up the Two Formulas
Incorrect:
matrix[i][i]
when trying to print the anti-diagonal.
Correct:
matrix[i][n - 1 - i]
Forgetting the -1
Incorrect:
matrix[i][n - i]
This causes an ArrayIndexOutOfBoundsException.
Correct:
matrix[i][n - 1 - i]
Using a Rectangular Matrix
Diagonal formulas are intended for square matrices.
Always verify:
matrix.length == matrix[0].length
Double Counting the Center Element
When calculating the sum of both diagonals in an odd-sized matrix, remember that the center element belongs to both diagonals.
Handle it carefully if you need the sum of distinct diagonal elements.
Expert Tips
- Memorize these two formulas:
- Main diagonal →
matrix[i][i] - Anti-diagonal →
matrix[i][n - 1 - i]
- Main diagonal →
- These formulas are frequently used in coding interviews.
- They form the basis for solving problems involving matrix trace, diagonal sums, identity matrices, and matrix rotations.
- A quick way to verify the anti-diagonal formula is:
- At
i = 0, the column should ben - 1. - At
i = n - 1, the column should be0.
- At
Comparison of Diagonals
| Diagonal | Formula | Direction |
|---|---|---|
| Main (Primary) | matrix[i][i] |
Top-left → Bottom-right |
| Anti (Secondary) | matrix[i][n - 1 - i] |
Top-right → Bottom-left |
Frequently Asked Questions
What is the formula for the main diagonal?
Use:
matrix[i][i]
What is the formula for the anti-diagonal?
Use:
matrix[i][n - 1 - i]
where n is the matrix size.
Can I print diagonals of a rectangular matrix?
These formulas are intended for square matrices. For rectangular matrices, diagonal concepts require different handling.
Why does the center element appear in both diagonals?
In an odd-sized square matrix, both diagonals intersect at the center element.
How do I calculate the sum of the main diagonal?
Traverse the main diagonal using:
matrix[i][i]
and add each element to a running total.
Is there a built-in Java method for printing diagonals?
No. You must implement the traversal yourself using loops and the appropriate index formulas.
Are there more than two diagonals in a matrix?
Yes. A matrix contains many diagonals parallel to the main diagonal, but the main and anti-diagonals are the most commonly used in programming.
Where are diagonal operations used?
Diagonal operations are widely used in linear algebra, matrix algorithms, computer graphics, scientific computing, and coding interview problems.