Check Rotated Matrix Equality (180 Degrees)

This program checks if one matrix is rotation of another by 180 degrees.

Problem Statement

Write a Java program to check whether matrix B is rotation of matrix A by 180 degrees.

Source Code

1 public class MatrixRotation180Check {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Matrix Rotation 180 Check");
4 int[][] A = {{1,2,3},{4,5,6},{7,8,9}};
5 int[][] B = {{9,8,7},{6,5,4},{3,2,1}};
6 boolean rotated=true;
7 for(int i=0;i<3;i++){
8 for(int j=0;j<3;j++){
9 if(B[i][j]!=A[3-1-i][3-1-j]) rotated=false;
10 }
11 }
12 if(rotated) System.out.println("Matrix B is 180-degree rotation of A");
13 else System.out.println("Matrix B is not rotation of A");
14 }
15 }

Program Output

Eduinq Matrix Rotation 180 Check
Matrix B is 180-degree rotation of A

Explanation

Rotation by 180 degrees maps each element to its diagonally opposite position, checked using B[i][j] == A[n-1-i][n-1-j], and nested loops validate this across the matrix, demonstrating a rotated matrix equality check.

Quick Links to Explore