Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Read the size n. If it is not from 2 to 10, print Invalid size and stop.
  2. Create the square array a of order n and read the elements row by row. Print the matrix.
  3. Set sumL = 0 and sumR = 0. For i from 0 to n - 1, print and add a[i][i] to sumL (the principal diagonal, where the row equals the column). Then, for the secondary diagonal, print and add a[i][n - 1 - i] to sumR (the column is n - 1 - i).
  4. 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.
  5. If n is odd, the two diagonals meet in the centre element a[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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
nintOrder of the square matrix
aint[][]Elements of the matrix
iintLoop counter over the rows (and along each diagonal)
jintLoop counter over the columns
sumLintSum of the principal (left) diagonal
sumRintSum 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) = 5

Sample 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 larger

Sample 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 larger

Sample 4

Enter the size of the matrix (2 to 10): 11
Invalid size

One value per line

Dry 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.

StepiElement usedsumLsumR
Start--00
Principal, i = 00a[0][0] = -1-10
Principal, i = 11a[1][1] = -4-50
Secondary, i = 00a[0][1] = 5-55
Secondary, i = 11a[1][0] = 3-58

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

  1. 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."
  2. Symmetric matrix "Check whether a square matrix is symmetric."
  3. Border elements "Display the boundary elements of a matrix and find their sum."
  4. Triangle sums "Find the sum of the elements above and below the principal diagonal."