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

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

  1. In main, read the rows m and columns c.
  2. If m != c, print NOT POSSIBLE and stop. This test comes first, so a non-square order such as 1 5 always gets this message.
  3. Otherwise, if m is outside 2 to 10, print Invalid size and stop.
  4. Otherwise create obj with new Symmetric(m). The constructor stores the order in n and creates the array a[n][n], so a non-square array can never be created.
  5. Call input(sc), which reads the elements with nested loops, using the Scanner passed to it, and display(), which prints the matrix row by row.
  6. Call isSymmetric():
    • The outer loop i runs from 0 to n - 1; the inner loop j starts at i + 1.
    • If a[i][j] != a[j][i], return false at once; if no pair differs, return true.
  7. Print It is a symmetric matrix or It is not a symmetric matrix according 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

VariableData TypePurpose
nintOrder of the square matrix, rows = columns (data member)
aint[][]Stores the elements of the matrix (data member)
sizeintConstructor parameter that receives the order of the matrix
scScannerReads the order in main; also the parameter of input() that carries the Scanner
mintRows typed by the user in main
cintColumns typed by the user in main
iintLoop counter that moves over the rows
jintLoop counter that moves over the columns
argsString[]Command line arguments of main; not used
objSymmetricObject 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 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: 11 11
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, 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():

ija[i][j]a[j][i]Equal?Action
0122YesContinue
0233YesContinue
1267NoReturn false

The method returns false, so the program prints It is not a symmetric matrix, the same as the second sample run.

Variations

  1. Identity matrix "Check whether a square matrix is an identity matrix."
  2. Transpose check "Find the transpose of a matrix and check whether it is equal to the original matrix."
  3. Triangles "Display the elements above and below the main diagonal of a square matrix."