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

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

  1. Read the number of rows m and columns n.
  2. If m != n, the matrix is not square, so print NOT POSSIBLE and stop. Check this before reading the elements.
  3. If the size is not from 2 to 10, print Invalid size and stop.
  4. Create the array a, read the elements row by row and print the matrix.
  5. Set sym = true. Compare each element above the diagonal with its mirror below it: for i from 0 to n - 1 and j from i + 1 to n - 1, if a[i][j] != a[j][i], set sym = false. Starting the inner loop at i + 1 compares each pair only once.
  6. If sym is true print It is a symmetric matrix, otherwise print It 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
mintNumber of rows of the matrix
nintNumber of columns of the matrix
aint[][]Elements of the matrix
iintLoop counter over the rows
jintLoop counter over the columns
symbooleanStays 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 matrix

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

Sample 3

Enter the number of rows and columns: 2 3
NOT POSSIBLE

Sample 4

Enter the number of rows and columns: 1 1
Invalid size

Sample 5

Enter the number of rows and columns: 1 5
NOT POSSIBLE

One value per line

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

ija[i][j]a[j][i]Equal?Action
0122YesContinue
0233YesContinue
1267NoSet 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

  1. Constructor version "Write a class with a parameterised constructor that receives the order of the matrix, and check whether it is symmetric."
  2. Identity matrix "Check whether a square matrix is an identity matrix."
  3. Transpose check "Find the transpose of a matrix and check whether it is equal to the original matrix."
  4. Triangles "Display the elements above and below the main diagonal of a square matrix."