In the previous article, we have discussed Java Program to Clockwise Rotate the Matrix Elements
In this article we are going to see how we can write a program to rotate the matrix 90 degree in JAVA language.
Java Program to Rotate the Matrix 90 degree
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
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to to rotate the Matrix 90 degree.
Method-1: Java Program to Rotate the Matrix 90 degree By Static Initialization of Array Elements
Approach:
- Initialize and an array of size 3×3, with elements.
- Transpose the matrix and then reverse the elements
- Print the matrix.
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("\nThe matrix elements are : "); printMatrix(arr); // Calls the transpose function inside the rotate array function rotateArr(trans(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 void 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; } System.out.print("\nThe rotated matrix is : "); printMatrix(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: The matrix elements are : 19 25 32 40 54 62 70 20 60 The rotated matrix is : 32 62 60 25 54 20 19 40 70
Method-2: Java Program to Rotate the Matrix 90 degree By Dynamic Initialization of Array Elements
Approach:
- Declare one array of size 3×3.
- Ask the user for input of array elements and store them in the array using two for loops.
- Transpose the matrix and then reverse the elements.
- Print the matrix.
Program:
import java.util.*; public class matrix { public static void main(String args[]) { //Scanner class 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 ; // Taking matrix1 input System.out.print("Enter matrix elements : "); for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.print("\nThe matrix elements are:"); printMatrix(arr); // Calls the transpose function inside the rotate array function rotateArr(trans(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 void 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; } System.out.print("\nThe rotated matrix is"); printMatrix(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]+" "); } } } }
Output: Enter matrix elements : 9 8 3 4 5 6 7 3 8 The matrix elements are: 9 8 3 4 5 6 7 3 8 The rotated matrix is 3 6 8 8 5 3 9 4 7
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: