ISC Practicals and Theory - Symmetric Matrix
Updated 21 Oct 2026
A symmetric matrix is a square matrix that is unchanged when its rows and columns are interchanged. This program, written in the main method alone, reads the order of a matrix, prints NOT POSSIBLE if it is not square, otherwise reads and prints the matrix and tells whether it is symmetric.
Definition
A matrix is symmetric if it is square (number of rows equals number of columns) and a[i][j] == a[j][i] for every pair i, j. In other words, it is equal to its transpose, and the elements are mirrored about the main diagonal.
For example, 1 2 3 / 2 5 6 / 3 6 9 is symmetric: a[0][1] = a[1][0] = 2, a[0][2] = a[2][0] = 3 and a[1][2] = a[2][1] = 6. If the last row were 3 7 9, then a[1][2] = 6 but a[2][1] = 7, so it would not be symmetric. A matrix of order $2 \times 3$ is not square, so the question of symmetry does not arise.
The diagonal elements a[i][i] are always equal to themselves, and the pair a[i][j], a[j][i] is the same as the pair a[j][i], a[i][j]. So it is enough to compare the elements above the diagonal (j > i) with their mirror elements; this program does exactly that.
Algorithm
- Read the number of rows
mand columnsn. - If
m != n, the matrix is not square, so printNOT POSSIBLEand stop. Check this before reading the elements. - If the size is not from 2 to 10, print
Invalid sizeand stop. - Create the array
a, read the elements row by row and print the matrix. - Set
sym = true. Compare each element above the diagonal with its mirror below it: forifrom 0 ton - 1andjfromi + 1ton - 1, ifa[i][j] != a[j][i], setsym = false. Starting the inner loop ati + 1compares each pair only once. - If
symis true printIt is a symmetric matrix, otherwise printIt is not a symmetric matrix.
Program
Everything is written inside main: the checks on the order, the input, the display and the pair-by-pair comparison above the diagonal.
import java.util.Scanner;
/* Program Symmetric reads a matrix and checks whether it is symmetric,
that is, a square matrix in which a[i][j] == a[j][i] for every i and j. */
public class Symmetric
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // reads the data from the keyboard
System.out.print("Enter the number of rows and columns: ");
int m = sc.nextInt(); // number of rows of the matrix
int n = sc.nextInt(); // number of columns of the matrix
if (m != n) // a matrix that is not square cannot be symmetric
{
System.out.println("NOT POSSIBLE");
}
else if (m < 2 || m > 10)
{
System.out.println("Invalid size");
}
else
{
int a[][] = new int[m][n]; // stores the elements of the matrix
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < m; i++) // i moves over the rows
{
for (int j = 0; j < n; j++) // j moves over the columns
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The matrix is:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println(); // new line after each row
}
boolean sym = true; // stays true while every pair a[i][j], a[j][i] is equal
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
{
sym = false;
}
}
}
if (sym)
{
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 |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
m | int | Number of rows of the matrix |
n | int | Number of columns of the matrix |
a | int[][] | Elements of the matrix |
i | int | Loop counter over the rows |
j | int | Loop counter over the columns |
sym | boolean | Stays true while every pair of mirror elements is equal |
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: 1 1
Invalid sizeSample 5
Enter the number of rows and columns: 1 5
NOT POSSIBLEDry run
Take the second sample: m = 3, n = 3, and the matrix 1 2 3 / 2 5 6 / 3 7 9. The matrix is square and the size is valid. The comparison loops run:
| 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 | Set sym = false |
sym is false, so the program prints It is not a symmetric matrix, the same as the second sample run.
Exam tip: Check that the matrix is square before reading the elements, and start the inner loop at i + 1 so that each pair is compared once.
Variations
- Constructor version "Write a class with a parameterised constructor that receives the order of the matrix, and check whether it is symmetric."
- 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."