Rotate matrix java – Java Program to Rotate the Matrix 180 degree

Rotate matrix java: In the previous article, we have discussed Java Program to Rotate the Matrix 90 degree

In this article we are going to see how we can write a program to rotate the matrix 180 degree in JAVA language.

Java Program to Rotate the Matrix 180 degree

Rotate matrix 90 degrees java: A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to rotate the Matrix 180 degree.

Method-1: Java Program to Rotate the Matrix 180 degree By Static Initialization of Array Elements

Approach:

  • Initialize and an array of size 3×3, with elements.
  • Print the matrix elements from last to first.

Program:

public class matrix
{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,32},{40,54,62},{70,20,60}};
        int row, col;

        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        System.out.print("\nThe matrix elements after 180 degree rotation are : ");
        printRotateMatrix(arr);
    }
    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
    // Function to print the rotatematrix
    static void printRotateMatrix(int arr[][])
    {
        int row, col;
        // Prints the elements from the last to first
        for(row=2;row>=0;row--)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=2;col>=0;col--)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }

}
Output:

The matrix elements are : 
19 25 32 
40 54 62 
70 20 60 
The matrix elements after 180 degree rotation are : 
60 20 70 
62 54 40 
32 25 19

Method-2: Java Program to Rotate the Matrix 180 degree By Dynamic Initialization of Array Elements

Approach:

  • Declare one array of size 3×3.
  • Take the array elements as input from the user.
  • Find the transpose of the array, then reverse its elements. Again repeat the step and now we will get the matrix 180 degree rotated.
  • Print the resultant matrix.

Program:

import java.util.Scanner;

public class matrix
{
    public static void main(String args[])
    {
        //Scanner class object created to take input
        Scanner scan = new Scanner(System.in);
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3];
        int row, col ,oddCounter = 0, evenCounter = 0;
        // Taking matrix1 input
        System.out.println("Enter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();

        System.out.print("The matrix elements are :");
        printMatrix(arr);
        // Calls the transpose function inside the rotate array function
        arr=rotateArr(trans(rotateArr(trans(arr))));
        System.out.print("\nThe rotated matrix elements are : ");
        printMatrix(arr);
    }

     // Method to find the transpose
    static int[][] trans(int[][] mat)
    {
        int row, col, trans[][] = new int[3][3];
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                trans[row][col] = mat[col][row];

        return trans;
    }
    
    // Reverses the array
    static int[][] rotateArr(int arr[][])
    {
        for(int i=0;i<3;i++)
            for(int j=0,k=2;j<k;j++,k--)
            {
                int temp = arr[j][i];
                arr[j][i] = arr[k][i];
                arr[k][i] = temp;
            }
            return arr;
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
         System.out.print("\n");
    }

}

Output:

Enter matrix elements : 1 2 3 4 5 6 7 8 9
The matrix elements are :
1 2 3 
4 5 6 
7 8 9

The rotated matrix elements are : 
9 8 7 
6 5 4 
3 2 1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: