Rotate Matrix 270 Degrees
This program rotates a matrix by 270 degrees clockwise (or 90 degrees counter clockwise).
Problem Statement
Write a Java program to rotate a 3x3 matrix by 270 degrees clockwise.
Source Code
| 1 | public class MatrixRotation270 { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Matrix Rotation 270 Degrees"); |
| 4 | int[][] A = {{1,2,3},{4,5,6},{7,8,9}}; |
| 5 | int[][] R = new int[3][3]; |
| 6 | for(int i=0;i<3;i++){ |
| 7 | for(int j=0;j<3;j++){ |
| 8 | R[3-1-j][i] = A[i][j]; |
| 9 | } |
| 10 | } |
| 11 | for(int i=0;i<3;i++){ |
| 12 | for(int j=0;j<3;j++){ |
| 13 | System.out.print(R[i][j]+" "); |
| 14 | } |
| 15 | System.out.println(); |
| 16 | } |
| 17 | } |
| 18 | } |
Program Output
Eduinq Matrix Rotation 270 Degrees 3 6 9 2 5 8 1 4 7
Explanation
Rotation by 270 degrees clockwise is equivalent to 90 degrees counter-clockwise, achieved using the formula R[n-1-j][i] = A[i][j], with nested loops performing the transformation to rotate the matrix.