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

ISC Practicals and Theory - Matrix Rotation 90 (OOP)

Updated 21 Oct 2026

This version rotates a square matrix 90 degrees clockwise using a class Rotate. The parameterised constructor creates the original and rotated arrays, and separate methods read the matrix, rotate it and display it.

Definition

A square matrix is rotated 90 degrees clockwise when its first row becomes the last column and its last row becomes the first column. For a matrix a of order $n$ and the rotated matrix b, the element in row $i$, column $j$ of a goes to row $j$, column $n-1-i$ of b: b[j][n-1-i] = a[i][j]. The matrix with rows 1 2 3, 4 5 6, 7 8 9 becomes 7 4 1, 8 5 2, 9 6 3.

This version rotates the matrix clockwise. For an anticlockwise rotation the rule is b[n-1-j][i] = a[i][j].

Algorithm

  1. In main, create the Scanner and read the order into size.
  2. If size is less than 2 or more than 10, print Invalid size and stop.
  3. Otherwise create the object ob with new Rotate(size). The constructor stores size in n and creates the arrays a[n][n] and b[n][n].
  4. Call ob.readMatrix(sc) to read the elements of a with two nested loops.
  5. Call ob.rotate(). For every i and j it stores a[i][j] in b[j][n - 1 - i].
  6. Call ob.display(ob.a, "Original matrix:") and then ob.display(ob.b, "Rotated matrix (90 degrees clockwise):"). The method prints the heading and the matrix row by row.

Program

The constructor Rotate(int) sets up the arrays, readMatrix(Scanner) reads the original matrix, rotate() fills b from a without any library method, and display(int[][], String) prints a matrix after a heading.

import java.util.Scanner;
/**
 * Class Rotate stores a square matrix of order n and rotates it
 * 90 degrees clockwise into a second array.
 */
public class Rotate
{
    int n;          // order of the square matrix
    int a[][];      // stores the original matrix
    int b[][];      // stores the rotated matrix

    /** Parameterised constructor: sets the order n and creates the arrays a and b. */
    Rotate(int size)
    {
        n = size;
        a = new int[n][n];
        b = 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();
            }
        }
    }

    /**
     * Rotates a by 90 degrees clockwise into b: the element in row i, column j
     * of a goes to row j, column n-1-i of b.
     */
    void rotate()
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                b[j][n - 1 - i] = a[i][j];   // first row of a becomes last column of b
            }
        }
    }

    /**
     * Prints a heading and then the matrix m row by row.
     * Parameters: m - the matrix to print, heading - the line printed before it.
     */
    void display(int m[][], String heading)
    {
        System.out.println(heading);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(m[i][j] + "\t");
            }
            System.out.println();
        }
    }

    /** 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
        {
            Rotate ob = new Rotate(size);      // object of the class
            ob.readMatrix(sc);
            ob.rotate();
            ob.display(ob.a, "Original matrix:");
            ob.display(ob.b, "Rotated matrix (90 degrees clockwise):");
        }
    }
}

Variable description table

VariableData TypePurpose
nintOrder of the square matrix (data member)
aint[][]Stores the original matrix (data member)
bint[][]Stores the rotated matrix (data member)
sizeintParameter of the constructor, and the order read in main
scScannerReads the order and the elements; passed to readMatrix as a parameter
iintLoop counter for the rows
jintLoop counter for the columns
mint[][]Parameter of display: the matrix to be printed
headingStringParameter of display: the line printed before the matrix
argsString[]Command line arguments of main; not used
obRotateObject 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
Original matrix:
1	2	3	
4	5	6	
7	8	9	
Rotated matrix (90 degrees clockwise):
7	4	1	
8	5	2	
9	6	3	

Sample 2

Enter the size of the matrix (2 to 10): 2
Enter the elements:
-1 5
3 -4
Original matrix:
-1	5	
3	-4	
Rotated matrix (90 degrees clockwise):
3	-1	
-4	5	

Sample 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 16
Original matrix:
1	2	3	4	
5	6	7	8	
9	10	11	12	
13	14	15	16	
Rotated matrix (90 degrees clockwise):
13	9	5	1	
14	10	6	2	
15	11	7	3	
16	12	8	4	

Sample 4

Enter the size of the matrix (2 to 10): 11
Invalid size

One value per line

Dry 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 two $2 \times 2$ arrays. In rotate(), b[j][1 - i] = a[i][j].

ija[i][j]Goes tob after the step
00-1b[0][1]row 0: 0 -1, row 1: 0 0
015b[1][1]row 0: 0 -1, row 1: 0 5
103b[0][0]row 0: 3 -1, row 1: 0 5
11-4b[1][0]row 0: 3 -1, row 1: -4 5

The rotated matrix has the rows 3 -1 and -4 5, the same as the second sample run.

Exam tip: Keep the result in a second array b; writing back into a while rotating overwrites elements that have not been moved yet.

Variations

  1. Anticlockwise rotation "Accept a square matrix and rotate it 90 degrees anticlockwise."
  2. Rotation by 180 degrees "Rotate a square matrix by 180 degrees and display it."
  3. Rotate and sum the diagonals "Rotate the matrix 90 degrees clockwise and display the sum of its diagonal elements."