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
- In
main, create theScannerand read the order intosize. - If
sizeis less than 2 or more than 10, printInvalid sizeand stop. - Otherwise create the object
obwithnew Diagonal(size). The constructor storessizeinnand creates the arraya[n][n]. - Call
ob.readMatrix(sc)to read the elements with two nested loops, andob.display()to print the matrix. - Call
ob.principal(). It printsa[i][i]for eachiand returns the sum, whichmainkeeps insumL. - Call
ob.secondary(). It printsa[i][n - 1 - i]for eachiand returns the sum, whichmainkeeps insumR. - Call
ob.compare(sumL, sumR)to print both sums and say which is larger, or that they are equal. - Call
ob.centre(), which printsa[n / 2][n / 2]only whennis 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
| Variable | Data Type | Purpose |
|---|---|---|
| n | int | Order of the square matrix (data member) |
| a | int[][] | Stores the elements of the matrix (data member) |
| size | int | Parameter of the constructor, and the order read in main |
| sc | Scanner | Reads the order and the elements; passed to readMatrix as a parameter |
| i | int | Loop counter for the rows, also the position along a diagonal |
| j | int | Loop counter for the columns |
| s | int | Running sum inside principal() and secondary() |
| sumL | int | Sum of the principal diagonal (parameter of compare, and the value kept in main) |
| sumR | int | Sum of the secondary diagonal (parameter of compare, and the value kept in main) |
| args | String[] | Command line arguments of main; not used |
| ob | Diagonal | Object 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) = 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): 1
Invalid sizeDry 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.
| Call | i | Element used | s |
|---|---|---|---|
| principal() starts | - | - | 0 |
| principal() | 0 | a[0][0] = -1 | -1 |
| principal() | 1 | a[1][1] = -4 | -5 |
| secondary() starts | - | - | 0 |
| secondary() | 0 | a[0][1] = 5 | 5 |
| secondary() | 1 | a[1][0] = 3 | 8 |
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
- 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."