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

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

  1. Read the order n. If it is not from 2 to 10, print Invalid size and stop.
  2. Read the angle ch. If it is not 180 or 270, print Invalid choice and stop.
  3. Create the arrays a (the original) and r (the rotated matrix) and read the elements of a row by row.
  4. For every i and j: if ch == 180, set r[i][j] = a[n - 1 - i][n - 1 - j]; otherwise (270 degrees) set r[i][j] = a[j][n - 1 - i].
  5. 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
nintOrder of the square matrix
chintChoice of rotation, 180 or 270 degrees
aint[][]The original matrix
rint[][]The rotated matrix
iintLoop counter over the rows
jintLoop 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 choice

Sample 4

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

One value per line

Dry 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].

ijElement usedr[i][j]
00a[0][2]3
01a[1][2]6
02a[2][2]9
10a[0][1]2
11a[1][1]5
12a[2][1]8
20a[0][0]1
21a[1][0]4
22a[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

  1. Rotation by 90 degrees "Rotate a square matrix by 90 degrees clockwise and display the original and the rotated matrix."
  2. Anticlockwise rotation "Rotate a square matrix by 90 degrees anticlockwise and display it."
  3. Rotation by any multiple of 90 "Accept an angle that is a multiple of 90 and rotate the matrix accordingly."