ISC Practicals and Theory - Diagonal Analysis
Updated 21 Oct 2026
A square matrix has two diagonals that run corner to corner. This program, written in the main method alone, reads a square matrix of order n, prints it, prints the elements of both diagonals with their sums, and says which sum is larger or whether they are equal.
Definition
In a square matrix of order $n$ stored in a two-dimensional array a:
- The principal (left) diagonal runs from the top-left corner to the bottom-right corner. Its elements are
a[i][i]for $i = 0, 1, ..., n-1$ (row number equals column number). - The secondary (right) diagonal runs from the top-right corner to the bottom-left corner. Its elements are
a[i][n-1-i]for $i = 0, 1, ..., n-1$ (row number plus column number equals $n-1$).
For the matrix with rows 1 2 3, 4 5 6, 7 8 9, the principal diagonal is $1, 5, 9$ (sum $15$) and the secondary diagonal is $3, 5, 7$ (sum $15$). When $n$ is odd the two diagonals cross at one centre element, here $5$.
Algorithm
- Read the size
n. If it is not from 2 to 10, printInvalid sizeand stop. - Create the square array
aof ordernand read the elements row by row. Print the matrix. - Set
sumL = 0andsumR = 0. Forifrom 0 ton - 1, print and adda[i][i]tosumL(the principal diagonal, where the row equals the column). Then, for the secondary diagonal, print and adda[i][n - 1 - i]tosumR(the column isn - 1 - i). - Print both sums and compare them: say that the principal diagonal sum is larger, that the secondary one is larger, or that they are equal.
- If
nis odd, the two diagonals meet in the centre elementa[n / 2][n / 2]; print it.
Program
Everything is written inside main: the input, the display, two loops for the diagonals and the comparison of the sums.
import java.util.Scanner;
public class DiagonalAnalysis
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter the size of the matrix (2 to 10): ");
int n = sc.nextInt(); // order of the square matrix
if (n < 2 || n > 10)
{
System.out.println("Invalid size");
}
else
{
int a[][] = new int[n][n]; // stores the elements of the matrix
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) // i runs over the rows
{
for (int j = 0; j < n; j++) // j runs over the columns
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The matrix is:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
int sumL = 0; // sum of the principal (left) diagonal
int sumR = 0; // sum of the secondary (right) diagonal
System.out.print("Principal diagonal:");
for (int i = 0; i < n; i++)
{
System.out.print(" " + a[i][i]); // principal diagonal: row = column
sumL = sumL + a[i][i];
}
System.out.println();
System.out.print("Secondary diagonal:");
for (int i = 0; i < n; i++)
{
System.out.print(" " + a[i][n - 1 - i]); // secondary diagonal: column = n-1-row
sumR = sumR + a[i][n - 1 - i];
}
System.out.println();
System.out.println("Sum of principal diagonal = " + sumL);
System.out.println("Sum of secondary diagonal = " + sumR);
if (sumL > sumR)
System.out.println("Principal diagonal sum is larger");
else if (sumR > sumL)
System.out.println("Secondary diagonal sum is larger");
else
System.out.println("Both diagonal sums are equal");
if (n % 2 == 1)
System.out.println("Centre element (common to both diagonals) = " + a[n / 2][n / 2]);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
n | int | Order of the square matrix |
a | int[][] | Elements of the matrix |
i | int | Loop counter over the rows (and along each diagonal) |
j | int | Loop counter over the columns |
sumL | int | Sum of the principal (left) diagonal |
sumR | int | Sum of the secondary (right) diagonal |
Sample input and output
Sample 1
Enter the size of the matrix (2 to 10): 3
Enter the elements:
1 2 3
4 5 6
7 8 9
The matrix is:
1 2 3
4 5 6
7 8 9
Principal diagonal: 1 5 9
Secondary diagonal: 3 5 7
Sum of principal diagonal = 15
Sum of secondary diagonal = 15
Both diagonal sums are equal
Centre element (common to both diagonals) = 5Sample 2
Enter the size of the matrix (2 to 10): 2
Enter the elements:
-1 5
3 -4
The matrix is:
-1 5
3 -4
Principal diagonal: -1 -4
Secondary diagonal: 5 3
Sum of principal diagonal = -5
Sum of secondary diagonal = 8
Secondary diagonal sum is largerSample 3
Enter the size of the matrix (2 to 10): 4
Enter the elements:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 17
The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 17
Principal diagonal: 1 6 11 17
Secondary diagonal: 4 7 10 13
Sum of principal diagonal = 35
Sum of secondary diagonal = 34
Principal diagonal sum is largerSample 4
Enter the size of the matrix (2 to 10): 11
Invalid sizeDry run
Take $n = 2$ with rows -1 5 and 3 -4, so a[0][0] = -1, a[0][1] = 5, a[1][0] = 3, a[1][1] = -4. Here n is 2, so the size is valid.
| Step | i | Element used | sumL | sumR |
|---|---|---|---|---|
| Start | - | - | 0 | 0 |
| Principal, i = 0 | 0 | a[0][0] = -1 | -1 | 0 |
| Principal, i = 1 | 1 | a[1][1] = -4 | -5 | 0 |
| Secondary, i = 0 | 0 | a[0][1] = 5 | -5 | 5 |
| Secondary, i = 1 | 1 | a[1][0] = 3 | -5 | 8 |
Since n is even, no centre element is printed. Comparing the sums, sumL = -5 is less than sumR = 8, so the last line is Secondary diagonal sum is larger, the same as the second sample run.
Exam tip: For the secondary diagonal the column is n - 1 - i, not n - i; the latter goes outside the array on the first row.
Variations
- Left and right diagonal sums "Accept a square matrix of order n and find the sum of the left diagonal and the sum of the right diagonal elements."
- Symmetric matrix "Check whether a square matrix is symmetric."
- Border elements "Display the boundary elements of a matrix and find their sum."
- Triangle sums "Find the sum of the elements above and below the principal diagonal."