ISC Practicals and Theory - Symmetric Matrix (OOP)
Updated 21 Oct 2026
A symmetric matrix is a square matrix in which a[i][j] == a[j][i] for every i and j. In this version, main reads the order and rejects a non-square matrix before any object exists; only a valid square order creates the object, through the parameterised constructor of class Symmetric.
Definition
A matrix is symmetric if it is square and equal to its transpose, that is, a[i][j] == a[j][i] for all i, j. For example, 1 2 / 2 3 is symmetric, while 1 2 / 5 3 is not. Only the elements above the main diagonal need to be compared with their mirror elements, since the diagonal elements are always equal to themselves.
Algorithm
- In
main, read the rowsmand columnsc. - If
m != c, printNOT POSSIBLEand stop. This test comes first, so a non-square order such as1 5always gets this message. - Otherwise, if
mis outside 2 to 10, printInvalid sizeand stop. - Otherwise create
objwithnew Symmetric(m). The constructor stores the order innand creates the arraya[n][n], so a non-square array can never be created. - Call
input(sc), which reads the elements with nested loops, using the Scanner passed to it, anddisplay(), which prints the matrix row by row. - Call
isSymmetric():- The outer loop
iruns from 0 ton - 1; the inner loopjstarts ati + 1. - If
a[i][j] != a[j][i], returnfalseat once; if no pair differs, returntrue.
- The outer loop
- Print
It is a symmetric matrixorIt is not a symmetric matrixaccording to the value returned.
Program
The constructor creates the array, input() and display() read and print it, and isSymmetric() compares each element above the diagonal with its mirror element.
import java.util.Scanner;
/* Class Symmetric stores a square matrix of order n x n and checks whether it is
symmetric, that is, a[i][j] == a[j][i] for every i and j. */
public class Symmetric
{
int n; // order of the square matrix (rows = columns = n)
int a[][]; // stores the elements of the matrix
/* Parameterised constructor: stores the order and creates the array.
Parameter: size is the number of rows (and columns) of the matrix. */
Symmetric(int size)
{
n = size;
a = new int[n][n];
}
/* Reads the elements of the matrix from the Scanner passed in.
Parameter: sc is the Scanner connected to the keyboard. */
void input(Scanner sc)
{
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < n; i++) // i moves over the rows
{
for (int j = 0; j < n; j++) // j moves 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(); // new line after each row
}
}
/* Compares every element above the main diagonal with its mirror element
below the diagonal. Returns true if all pairs are equal, otherwise false. */
boolean isSymmetric()
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++) // only the upper half: j > i
{
if (a[i][j] != a[j][i]) // one unequal pair is enough
{
return false;
}
}
}
return true;
}
/* main reads the order, checks it, then creates the object and calls the methods. */
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows and columns: ");
int m = sc.nextInt(); // rows typed by the user
int c = sc.nextInt(); // columns typed by the user
if (m != c)
{
System.out.println("NOT POSSIBLE");
}
else if (m < 2 || m > 10)
{
System.out.println("Invalid size");
}
else
{
Symmetric obj = new Symmetric(m);
obj.input(sc);
obj.display();
if (obj.isSymmetric())
{
System.out.println("It is a symmetric matrix");
}
else
{
System.out.println("It is not a symmetric matrix");
}
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| n | int | Order of the square matrix, rows = columns (data member) |
| a | int[][] | Stores the elements of the matrix (data member) |
| size | int | Constructor parameter that receives the order of the matrix |
| sc | Scanner | Reads the order in main; also the parameter of input() that carries the Scanner |
| m | int | Rows typed by the user in main |
| c | int | Columns typed by the user in main |
| i | int | Loop counter that moves over the rows |
| j | int | Loop counter that moves over the columns |
| args | String[] | Command line arguments of main; not used |
| obj | Symmetric | Object created in main to call the methods |
Sample input and output
Sample 1
Enter the number of rows and columns: 3 3
Enter the elements of the matrix:
1 2 3
2 5 6
3 6 9
The matrix is:
1 2 3
2 5 6
3 6 9
It is a symmetric matrixSample 2
Enter the number of rows and columns: 3 3
Enter the elements of the matrix:
1 2 3
2 5 6
3 7 9
The matrix is:
1 2 3
2 5 6
3 7 9
It is not a symmetric matrixSample 3
Enter the number of rows and columns: 2 3
NOT POSSIBLESample 4
Enter the number of rows and columns: 11 11
Invalid sizeSample 5
Enter the number of rows and columns: 1 5
NOT POSSIBLEDry run
Take the second sample: m = 3, c = 3, so the object is created with size = 3 and n = 3. The matrix is 1 2 3 / 2 5 6 / 3 7 9. Inside isSymmetric():
| i | j | a[i][j] | a[j][i] | Equal? | Action |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 2 | Yes | Continue |
| 0 | 2 | 3 | 3 | Yes | Continue |
| 1 | 2 | 6 | 7 | No | Return false |
The method returns false, so the program prints It is not a symmetric matrix, the same as the second sample run.
Variations
- Identity matrix "Check whether a square matrix is an identity matrix."
- Transpose check "Find the transpose of a matrix and check whether it is equal to the original matrix."
- Triangles "Display the elements above and below the main diagonal of a square matrix."