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

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

  1. Read the size n. If it is not from 2 to 10, print Invalid size and stop.
  2. Create the arrays a (the original) and b (the rotated matrix), both of order n, and read the elements of a row by row.
  3. Rotate: for every i and j, copy a[i][j] to b[j][n - 1 - i]. The first row of a becomes the last column of b, the second row the second-last column, and so on.
  4. Print the original matrix a and then the rotated matrix b, 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
nintOrder of the square matrix
aint[][]The original matrix
bint[][]The rotated matrix
iintLoop counter over the rows
jintLoop 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 size

One value per line

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

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: Write the result into a second array. Rotating a into itself 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. Transpose then reverse "Find the transpose of a matrix and reverse each row to get the clockwise rotation."
  4. Rotate and sum the diagonals "Rotate the matrix 90 degrees clockwise and display the sum of the elements of its diagonals."