ISC Practicals and Theory - Matrix Rotation 180/270
Updated 21 Oct 2026
Rotating a square matrix turns it about its centre so that every element moves to a new position. This program, written in the main method alone, reads an n x n matrix and a choice of 180 or 270 degrees, builds the rotated matrix in a second array using index arithmetic, and prints both matrices.
Definition
The direction of rotation matters, so this note fixes it. Rotation by 180 degrees gives the same result clockwise or anticlockwise: the matrix is turned upside down, so the first row becomes the last row written backwards. Rotation by 270 degrees clockwise is the same as 90 degrees anticlockwise: the last column becomes the first row, read from top to bottom.
For a matrix of order $n$, with the new matrix called $r$ and the original $a$:
- 180 degrees:
r[i][j] = a[n-1-i][n-1-j] - 270 degrees clockwise:
r[i][j] = a[j][n-1-i]
For example, take the rows 1 2 and 3 4. Rotated by 180 degrees they become 4 3 and 2 1. Rotated by 270 degrees clockwise they become 2 4 and 1 3.
Any choice other than 180 or 270 is treated as invalid input, and so is an order outside 2 to 10.
Algorithm
- Read the order
n. If it is not from 2 to 10, printInvalid sizeand stop. - Read the angle
ch. If it is not 180 or 270, printInvalid choiceand stop. - Create the arrays
a(the original) andr(the rotated matrix) and read the elements ofarow by row. - For every
iandj: ifch == 180, setr[i][j] = a[n - 1 - i][n - 1 - j]; otherwise (270 degrees) setr[i][j] = a[j][n - 1 - i]. - Print the original matrix and then the rotated matrix, a tab after each element and a new line after each row.
Program
Everything is written inside main: the two checks, the input, the nested loops with the two index formulas, 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);
System.out.print("Enter the order n of the matrix (2 to 10): ");
int n = sc.nextInt(); // order of the square matrix (rows = columns = n)
if (n < 2 || n > 10) // size out of range
{
System.out.println("Invalid size");
return;
}
System.out.print("Enter the angle of rotation (180 or 270): ");
int ch = sc.nextInt(); // choice of rotation: 180 or 270 degrees
if (ch != 180 && ch != 270) // only these two angles are allowed
{
System.out.println("Invalid choice");
return;
}
int a[][] = new int[n][n]; // stores the original matrix
int r[][] = new int[n][n]; // stores the rotated matrix
System.out.println("Enter the elements of the matrix:");
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++)
{
if (ch == 180)
r[i][j] = a[n - 1 - i][n - 1 - j];
else
r[i][j] = a[j][n - 1 - i];
}
}
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"); // tab between elements
}
System.out.println(); // new line after each row
}
System.out.println("Matrix rotated by " + ch + " degrees:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(r[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 |
ch | int | Choice of rotation, 180 or 270 degrees |
a | int[][] | The original matrix |
r | 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 order n of the matrix (2 to 10): 3
Enter the angle of rotation (180 or 270): 270
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
Original matrix:
1 2 3
4 5 6
7 8 9
Matrix rotated by 270 degrees:
3 6 9
2 5 8
1 4 7 Sample 2
Enter the order n of the matrix (2 to 10): 2
Enter the angle of rotation (180 or 270): 180
Enter the elements of the matrix:
-1 5
0 -1
Original matrix:
-1 5
0 -1
Matrix rotated by 180 degrees:
-1 0
5 -1 Sample 3
Enter the order n of the matrix (2 to 10): 3
Enter the angle of rotation (180 or 270): 90
Invalid choiceSample 4
Enter the order n of the matrix (2 to 10): 11
Invalid sizeDry run
Take $n = 3$, ch = 270 and the matrix with rows 1 2 3, 4 5 6, 7 8 9. Since ch is 270, the rotation uses r[i][j] = a[j][2-i].
| i | j | Element used | r[i][j] |
|---|---|---|---|
| 0 | 0 | a[0][2] | 3 |
| 0 | 1 | a[1][2] | 6 |
| 0 | 2 | a[2][2] | 9 |
| 1 | 0 | a[0][1] | 2 |
| 1 | 1 | a[1][1] | 5 |
| 1 | 2 | a[2][1] | 8 |
| 2 | 0 | a[0][0] | 1 |
| 2 | 1 | a[1][0] | 4 |
| 2 | 2 | a[2][0] | 7 |
The rotated matrix has rows 3 6 9, 2 5 8 and 1 4 7, the same as the first sample run.
Exam tip: Write the rotated elements into a second array. If you copy back into the same array while rotating, you overwrite elements you still need.
Variations
- Rotation by 90 degrees "Rotate a square matrix by 90 degrees clockwise and display the original and the rotated matrix."
- Anticlockwise rotation "Rotate a square matrix by 90 degrees anticlockwise and display it."
- Rotation by any multiple of 90 "Accept an angle that is a multiple of 90 and rotate the matrix accordingly."