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

ISC Practicals and Theory - Diagonal Analysis (OOP)

Updated 21 Oct 2026

This version finds the elements and sums of the two diagonals of a square matrix using a class Diagonal. The class has a parameterised constructor that creates the array, and separate methods that read, display, find each diagonal and compare the sums.

Definition

In a square matrix of order $n$ stored in an array a, the principal (left) diagonal is a[i][i] and the secondary (right) diagonal is a[i][n-1-i], for $i = 0, 1, ..., n-1$. For the rows 1 2 3, 4 5 6, 7 8 9 the diagonals are $1, 5, 9$ and $3, 5, 7$, each with sum $15$. For odd $n$ both diagonals pass through the centre element.

Algorithm

  1. In main, create the Scanner and read the order into size.
  2. If size is less than 2 or more than 10, print Invalid size and stop.
  3. Otherwise create the object ob with new Diagonal(size). The constructor stores size in n and creates the array a[n][n].
  4. Call ob.readMatrix(sc) to read the elements with two nested loops, and ob.display() to print the matrix.
  5. Call ob.principal(). It prints a[i][i] for each i and returns the sum, which main keeps in sumL.
  6. Call ob.secondary(). It prints a[i][n - 1 - i] for each i and returns the sum, which main keeps in sumR.
  7. Call ob.compare(sumL, sumR) to print both sums and say which is larger, or that they are equal.
  8. Call ob.centre(), which prints a[n / 2][n / 2] only when n is odd.

Program

The constructor Diagonal(int) sets up the array, readMatrix(Scanner) and display() handle input and output, principal() and secondary() each print one diagonal and return its sum, compare(int, int) reports the result, and centre() prints the common element for odd n.

import java.util.Scanner;
/**
 * Class Diagonal stores a square matrix of order n and finds the
 * elements and sums of its principal and secondary diagonals.
 */
public class Diagonal
{
    int n;          // order of the square matrix
    int a[][];      // stores the elements of the matrix

    /** Parameterised constructor: sets the order n and creates the n x n array. */
    Diagonal(int size)
    {
        n = size;
        a = new int[n][n];
    }

    /** Reads the n x n elements from the keyboard into the array a. */
    void readMatrix(Scanner sc)
    {
        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();
            }
        }
    }

    /** Prints the matrix row by row. */
    void display()
    {
        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();
        }
    }

    /** Prints the principal diagonal and returns its sum. */
    int principal()
    {
        int s = 0;      // running sum
        System.out.print("Principal diagonal:");
        for (int i = 0; i < n; i++)
        {
            System.out.print(" " + a[i][i]);   // row = column
            s = s + a[i][i];
        }
        System.out.println();
        return s;
    }

    /** Prints the secondary diagonal and returns its sum. */
    int secondary()
    {
        int s = 0;      // running sum
        System.out.print("Secondary diagonal:");
        for (int i = 0; i < n; i++)
        {
            System.out.print(" " + a[i][n - 1 - i]);   // column = n-1-row
            s = s + a[i][n - 1 - i];
        }
        System.out.println();
        return s;
    }

    /** Prints both sums (parameters) and tells which is larger, or that they are equal. */
    void compare(int sumL, int sumR)
    {
        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");
    }

    /** For odd n, prints the centre element common to both diagonals. */
    void centre()
    {
        if (n % 2 == 1)
            System.out.println("Centre element (common to both diagonals) = " + a[n / 2][n / 2]);
    }

    /** Reads n, checks it, then creates the object and calls the methods. */
    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 size = sc.nextInt();               // order entered by the user
        if (size < 2 || size > 10)
        {
            System.out.println("Invalid size");
        }
        else
        {
            Diagonal ob = new Diagonal(size);  // object of the class
            ob.readMatrix(sc);
            ob.display();
            int sumL = ob.principal();         // sum of principal diagonal
            int sumR = ob.secondary();         // sum of secondary diagonal
            ob.compare(sumL, sumR);
            ob.centre();
        }
    }
}

Variable description table

VariableData TypePurpose
nintOrder of the square matrix (data member)
aint[][]Stores the elements of the matrix (data member)
sizeintParameter of the constructor, and the order read in main
scScannerReads the order and the elements; passed to readMatrix as a parameter
iintLoop counter for the rows, also the position along a diagonal
jintLoop counter for the columns
sintRunning sum inside principal() and secondary()
sumLintSum of the principal diagonal (parameter of compare, and the value kept in main)
sumRintSum of the secondary diagonal (parameter of compare, and the value kept in main)
argsString[]Command line arguments of main; not used
obDiagonalObject of the class created in main

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): 1
Invalid size

One value per line

Dry run

Take $n = 2$ with rows -1 5 and 3 -4. main reads size = 2, which is valid, so the constructor sets n = 2 and creates a $2 \times 2$ array.

CalliElement useds
principal() starts--0
principal()0a[0][0] = -1-1
principal()1a[1][1] = -4-5
secondary() starts--0
secondary()0a[0][1] = 55
secondary()1a[1][0] = 38

So sumL = -5 and sumR = 8. compare(-5, 8) finds sumL less than sumR and prints Secondary diagonal sum is larger, the same as the second sample run. centre() prints nothing because n is even.

Exam tip: A parameterised constructor receives the order from main, so the array can be created the moment the object is made.

Variations

  1. Symmetric matrix "Check whether a square matrix is symmetric."
  2. Border elements "Display the boundary elements of a matrix and find their sum."
  3. Triangle sums "Find the sum of the elements above and below the principal diagonal."