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
- In
main, create theScannerand read the order intosize. - If
sizeis less than 2 or more than 10, printInvalid sizeand stop. - Otherwise create the object
obwithnew Rotate(size). The constructor storessizeinnand creates the arraysa[n][n]andb[n][n]. - Call
ob.readMatrix(sc)to read the elements ofawith two nested loops. - Call
ob.rotate(). For everyiandjit storesa[i][j]inb[j][n - 1 - i]. - Call
ob.display(ob.a, "Original matrix:")and thenob.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
| Variable | Data Type | Purpose |
|---|---|---|
| n | int | Order of the square matrix (data member) |
| a | int[][] | Stores the original matrix (data member) |
| b | int[][] | Stores the rotated matrix (data member) |
| size | int | Parameter of the constructor, and the order read in main |
| sc | Scanner | Reads the order and the elements; passed to readMatrix as a parameter |
| i | int | Loop counter for the rows |
| j | int | Loop counter for the columns |
| m | int[][] | Parameter of display: the matrix to be printed |
| heading | String | Parameter of display: the line printed before the matrix |
| args | String[] | Command line arguments of main; not used |
| ob | Rotate | Object 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 sizeDry 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].
| i | j | a[i][j] | Goes to | b after the step |
|---|---|---|---|---|
| 0 | 0 | -1 | b[0][1] | row 0: 0 -1, row 1: 0 0 |
| 0 | 1 | 5 | b[1][1] | row 0: 0 -1, row 1: 0 5 |
| 1 | 0 | 3 | b[0][0] | row 0: 3 -1, row 1: 0 5 |
| 1 | 1 | -4 | b[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
- Anticlockwise rotation "Accept a square matrix and rotate it 90 degrees anticlockwise."
- Rotation by 180 degrees "Rotate a square matrix by 180 degrees and display it."
- Rotate and sum the diagonals "Rotate the matrix 90 degrees clockwise and display the sum of its diagonal elements."