SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Rotate Matrix 90 Degrees Clockwise Program in Java and Python

16 February 2015

ISC 2015 Question 2 solution to rotate a square matrix 90 degrees clockwise, display the original matrix and find the sum of the four corner elements.

Question:

Write a program to declare a square matrix A[][] of order M x M, where M is the number of rows and the number of columns, such that M must be greater than 2 and less than 10. Accept the value of M as user input. Display an appropriate message for an invalid input. Allow the user to input integers into this matrix.

Perform the following tasks:

  1. Display the original matrix.
  2. Rotate the matrix 90 degrees clockwise.
  3. Find the sum of the elements of the four corners of the matrix.

For example, if the original matrix is:

1 2 3 4 5 6 7 8 9

then the matrix after 90 degrees clockwise rotation becomes:

7 4 1 8 5 2 9 6 3

Test your program for the following data and some random data:

Example 1

INPUT: M = 3 3 4 9 2 5 8 1 6 7 OUTPUT: ORIGINAL MATRIX 3 4 9 2 5 8 1 6 7 MATRIX AFTER ROTATION 1 2 3 6 5 4 7 8 9 Sum of the corner elements = 20

Example 2

INPUT: M = 4 1 2 4 9 2 5 8 3 1 6 7 4 3 7 6 5 OUTPUT: ORIGINAL MATRIX 1 2 4 9 2 5 8 3 1 6 7 4 3 7 6 5 MATRIX AFTER ROTATION 3 1 2 1 7 6 5 2 6 7 8 4 5 4 3 9 Sum of the corner elements = 18

Example 3

INPUT: M = 14 OUTPUT: SIZE OUT OF RANGE

Algorithm:

Step 1: Start.

Step 2: Create a Scanner object to accept input from the user.

Step 3: Accept the order of the square matrix in the variable m.

Step 4: If m < 3 or m > 9, display Size Out Of Range and go to Step 15.

Step 5: Declare an integer matrix A of size m x m.

Step 6: Use two nested loops to accept all elements of the matrix row by row.

Step 7: Display a heading for the original matrix.

Step 8: Use two nested loops from row 0 to m - 1 and column 0 to m - 1 to print the original matrix.

Step 9: Display a heading for the rotated matrix.

Step 10: Run the outer loop with column index i from 0 to m - 1.

Step 11: For each i, run the inner loop with row index j from m - 1 down to 0.

Step 12: Print A[j][i] to display one row of the clockwise rotated matrix.

Step 13: Add A[0][0], A[0][m - 1], A[m - 1][0] and A[m - 1][m - 1] to get the sum of the four corner elements.

Step 14: Display the sum of the corner elements.

Step 15: Stop.

Explanation:

This program works with a square matrix, so the number of rows and columns is the same. The value of M is first checked because the question allows only orders greater than 2 and less than 10. Therefore valid matrix sizes are 3 to 9. If the size is outside this range, the program does not try to create or fill the matrix; it simply prints the error message. This prevents unnecessary input and keeps the program according to the ISC question.

After a valid size is entered, the program stores the elements in a two-dimensional integer array. The first pair of nested loops is used only for input. The outer loop represents the row number and the inner loop represents the column number, so the values are accepted in normal row-wise order. The same style of nested loops is then used to print the original matrix exactly as it was entered.

The main idea of the rotation is based on the relationship between rows and columns. In a 90 degrees clockwise rotation, the first row of the rotated matrix is formed by taking the first column of the original matrix from bottom to top. The second row of the rotated matrix is formed by taking the second column from bottom to top, and so on. That is why the program keeps i moving from the first column to the last column, while j moves from the last row to the first row. Printing A[j][i] in this order displays the rotated matrix without needing a second array.

The corner sum is calculated from the original matrix. The four corner positions are fixed for any square matrix: top-left, top-right, bottom-left and bottom-right. Using array indexes, these are A[0][0], A[0][m - 1], A[m - 1][0] and A[m - 1][m - 1]. Adding these four values gives the required sum. The program then displays the result after the rotated matrix.

Java Program:

Java
/**
* The class Q2_ISC2015 inputs a square matrix and rotates it
* 90 degrees clockwise. It also displays the sum of the corner elements.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2015 Question 2
*/

import java.util.Scanner;

class Q2_ISC2015
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the size of the matrix : ");
        int m = sc.nextInt();

        if(m < 3 || m > 9)
        {
            System.out.println("Size Out Of Range");
        }
        else
        {
            int A[][] = new int[m][m];

            // Input the matrix elements row by row.
            for(int i = 0; i < m; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    System.out.print("Enter an element : ");
                    A[i][j] = sc.nextInt();
                }
            }

            // Display the original matrix.
            System.out.println("*************************");
            System.out.println("The Original Matrix is : ");
            for(int i = 0; i < m; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    System.out.print(A[i][j] + "\t");
                }
                System.out.println();
            }
            System.out.println("*************************");

            /*
            * To rotate clockwise, print every column from
            * bottom to top, starting with the first column.
            */
            System.out.println("Matrix After Rotation is : ");
            for(int i = 0; i < m; i++)
            {
                for(int j = m - 1; j >= 0; j--)
                {
                    System.out.print(A[j][i] + "\t");
                }
                System.out.println();
            }
            System.out.println("*************************");

            // Add the four corner elements of the original matrix.
            int sum = A[0][0] + A[0][m - 1] + A[m - 1][0] + A[m - 1][m - 1];
            System.out.println("Sum of the corner elements = " + sum);
        }
    }
}

Equivalent Python Program:

Python
# Program to rotate a square matrix 90 degrees clockwise
# and find the sum of its four corner elements.

m = int(input("Enter the size of the matrix : "))

if m < 3 or m > 9:
    print("Size Out Of Range")
else:
    matrix = []

    # Input the matrix elements row by row.
    for i in range(m):
        row = []
        for j in range(m):
            value = int(input("Enter an element : "))
            row.append(value)
        matrix.append(row)

    # Display the original matrix.
    print("*************************")
    print("The Original Matrix is : ")
    for i in range(m):
        for j in range(m):
            print(matrix[i][j], end="\t")
        print()
    print("*************************")

    # Print each column from bottom to top to rotate clockwise.
    print("Matrix After Rotation is : ")
    for i in range(m):
        for j in range(m - 1, -1, -1):
            print(matrix[j][i], end="\t")
        print()
    print("*************************")

    # Add the four corner elements of the original matrix.
    corner_sum = matrix[0][0] + matrix[0][m - 1] + matrix[m - 1][0] + matrix[m - 1][m - 1]
    print("Sum of the corner elements =", corner_sum)

Output:

Enter the size of the matrix : 4 Enter an element : 1 Enter an element : 2 Enter an element : 4 Enter an element : 9 Enter an element : 2 Enter an element : 5 Enter an element : 8 Enter an element : 3 Enter an element : 1 Enter an element : 6 Enter an element : 7 Enter an element : 4 Enter an element : 3 Enter an element : 7 Enter an element : 6 Enter an element : 5 ************************* The Original Matrix is : 1 2 4 9 2 5 8 3 1 6 7 4 3 7 6 5 ************************* Matrix After Rotation is : 3 1 2 1 7 6 5 2 6 7 8 4 5 4 3 9 ************************* Sum of the corner elements = 18

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →