ISC Practicals and Theory - Matrix Rotation 90
Updated 21 Oct 2026
Rotating a matrix by 90 degrees turns its rows into columns. This program, written in the main method alone, reads a square matrix of order n, rotates it 90 degrees clockwise into a second array, and prints the original and the rotated matrix.
Definition
A square matrix is rotated 90 degrees clockwise when its first row becomes the last column, its second row becomes the second last column, and so on, with the last row becoming the first column. Each row is written into a column from top to bottom.
For a matrix a of order $n$ and the rotated matrix b, the element in row $i$ and column $j$ of a goes to row $j$ and column $n-1-i$ of b:
b[j][n-1-i] = a[i][j]
For example, the matrix with rows 1 2 3, 4 5 6, 7 8 9 becomes 7 4 1, 8 5 2, 9 6 3. The first row 1 2 3 is now the last column.
This program rotates the matrix clockwise. For an anticlockwise rotation the rule is b[n-1-j][i] = a[i][j].
Algorithm
- Read the size
n. If it is not from 2 to 10, printInvalid sizeand stop. - Create the arrays
a(the original) andb(the rotated matrix), both of ordern, and read the elements ofarow by row. - Rotate: for every
iandj, copya[i][j]tob[j][n - 1 - i]. The first row ofabecomes the last column ofb, the second row the second-last column, and so on. - Print the original matrix
aand then the rotated matrixb, a tab after each element and a new line after each row.
Program
Everything is written inside main: the input, the nested loops that rotate into the second array, and the loops that print both matrices.
import java.util.Scanner;
public class MatrixRotation
{
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 n = sc.nextInt(); // order of the square matrix
if (n < 2 || n > 10)
{
System.out.println("Invalid size");
}
else
{
int a[][] = new int[n][n]; // stores the original matrix
int b[][] = new int[n][n]; // stores the rotated matrix
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();
}
}
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
}
}
System.out.println("Original matrix:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
System.out.println("Rotated matrix (90 degrees clockwise):");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
}
}
}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 |
n | int | Order of the square matrix |
a | int[][] | The original matrix |
b | int[][] | The rotated matrix |
i | int | Loop counter over the rows |
j | int | Loop counter over the columns |
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): 0
Invalid sizeDry run
Take $n = 2$ with rows -1 5 and 3 -4, so a[0][0] = -1, a[0][1] = 5, a[1][0] = 3, a[1][1] = -4. The size is valid, so the rotation uses 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: Write the result into a second array. Rotating a into itself 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."
- Transpose then reverse "Find the transpose of a matrix and reverse each row to get the clockwise rotation."
- Rotate and sum the diagonals "Rotate the matrix 90 degrees clockwise and display the sum of the elements of its diagonals."