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

ISC Practicals and Theory - Matrix Rotation 180/270 (OOP)

Updated 21 Oct 2026

This version of the matrix rotation program is built around a class. A parameterised constructor receives the order and the angle and creates the arrays, and separate methods rotate180() and rotate270() each do one rotation. The program reads an n x n matrix and an angle of 180 or 270 degrees, prints the original matrix and the rotated one.

Definition

Rotation by 180 degrees is the same clockwise or anticlockwise: the matrix is turned upside down. Rotation by 270 degrees clockwise is the same as 90 degrees anticlockwise. For order $n$, with original $a$ and rotated $r$:

  • 180 degrees: r[i][j] = a[n-1-i][n-1-j]
  • 270 degrees clockwise: r[i][j] = a[j][n-1-i]

For the rows 1 2 and 3 4, 180 degrees gives 4 3 and 2 1, and 270 degrees gives 2 4 and 1 3. An order outside 2 to 10, or an angle other than 180 or 270, is invalid.

Algorithm

  1. In main, read the order into size. If it is less than 2 or more than 10, print Invalid size and stop.
  2. Read the angle into angle. If it is neither 180 nor 270, print Invalid choice and stop.
  3. Create the object with new MatrixRotation(size, angle). The constructor stores n and ch and creates the arrays a and r of size n x n.
  4. Call readMatrix() to read the elements into a.
  5. If angle is 180 call rotate180(), otherwise call rotate270(). Both fill r from a with two nested loops over i and j; a is never changed.
  6. Print Original matrix: and call display(a), then print the heading with the angle and call display(r).

Program

The constructor sets up the data members, readMatrix() reads the elements, rotate180() and rotate270() build the rotated matrix, and display() prints a matrix.

import java.util.Scanner;
/**
 * Class MatrixRotation (OOP version) stores an n x n matrix and rotates it
 * by 180 or 270 degrees into a second matrix.
 * 180 degrees is the same clockwise or anticlockwise;
 * 270 degrees clockwise is the same as 90 degrees anticlockwise.
 */
public class MatrixRotation
{
    int a[][];   // stores the original matrix
    int r[][];   // stores the rotated matrix
    int n;       // order of the square matrix (rows = columns = n)
    int ch;      // choice of rotation: 180 or 270 degrees

    /**
     * Parameterised constructor.
     * nn is the order of the matrix, cc the angle (180 or 270).
     * Stores them and creates the two n x n arrays.
     */
    MatrixRotation(int nn, int cc)
    {
        n = nn;
        ch = cc;
        a = new int[n][n];
        r = new int[n][n];
    }

    /**
     * Reads the n x n elements into a[][].
     */
    void readMatrix(Scanner sc)
    {
        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();
            }
        }
    }

    /**
     * Rotates by 180 degrees: the element at row i, column j
     * comes from row n-1-i, column n-1-j of the original.
     */
    void rotate180()
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                r[i][j] = a[n - 1 - i][n - 1 - j];
            }
        }
    }

    /**
     * Rotates by 270 degrees clockwise (90 degrees anticlockwise):
     * the element at row i, column j comes from row j, column n-1-i.
     */
    void rotate270()
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                r[i][j] = a[j][n - 1 - i];
            }
        }
    }

    /**
     * Prints any n x n matrix x[][] row by row.
     */
    void display(int x[][])
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(x[i][j] + "\t");   // tab between elements
            }
            System.out.println();                   // new line after each row
        }
    }

    /**
     * main reads and checks n and the angle, creates the object
     * and calls the methods in order.
     */
    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 size = sc.nextInt();
        if (size < 2 || size > 10)            // size out of range
        {
            System.out.println("Invalid size");
        }
        else
        {
            System.out.print("Enter the angle of rotation (180 or 270): ");
            int angle = sc.nextInt();
            if (angle != 180 && angle != 270) // only these two angles are allowed
            {
                System.out.println("Invalid choice");
            }
            else
            {
                MatrixRotation obj = new MatrixRotation(size, angle);
                obj.readMatrix(sc);
                if (angle == 180)
                    obj.rotate180();
                else
                    obj.rotate270();
                System.out.println("Original matrix:");
                obj.display(obj.a);
                System.out.println("Matrix rotated by " + angle + " degrees:");
                obj.display(obj.r);
            }
        }
    }
}

Variable description table

VariableData TypePurpose
aint[][]Data member; stores the original matrix
rint[][]Data member; stores the rotated matrix
nintData member; order of the square matrix
chintData member; angle of rotation, 180 or 270
nnintConstructor parameter; order passed from main
ccintConstructor parameter; angle passed from main
scScannerReads the order, the angle and the elements (in main and as a parameter of readMatrix())
xint[][]Parameter of display(); the matrix to be printed
iintLoop counter for the rows
jintLoop counter for the columns
argsString[]Command line arguments of main; not used
sizeintOrder of the matrix read in main
angleintAngle read in main
objMatrixRotationObject created in main that calls the methods

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 = 2$, angle = 180 and the matrix with rows -1 5 and 0 -1. rotate180() uses r[i][j] = a[1-i][1-j].

ijElement usedr[i][j]
00a[1][1]-1
01a[1][0]0
10a[0][1]5
11a[0][0]-1

The rotated matrix has rows -1 0 and 5 -1, the same as the second sample run.

Exam tip: Check the order and the angle in main before creating the object, so the constructor only ever receives valid values.

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."