logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blog
  • Crypto Tools
  • Dev Feed
  • Login
Story
Follow @devglan

Write a Java program to rotate a matrix

author-image   By Dhiraj,   10 May, 2020 5K

Given a 2D matrix of N X N. Write a Java program to rotate the matrix in a clockwise direction by 90 degrees. The 0th row of the given matrix will be transformed to the nth column, the 1st row will be transformed to the n-1 column, and so on. Below is its representation.

matrix-rotation

To solve this problem, we will first find the transpose of the matrix and then reverse the elements of each row. This will result in a matrix rotated in a clockwise direction by 90 degrees.

The transpose of a matrix is a new matrix whose rows are the columns of the original.

One thing to be noted here is the nested loop that is starting with i. To understand the reason, you can refer the image at the end of the article.

Below is the Java program for the same.

public class Matrix {

    public static void rotateMatrix(int[][] matrix){
        int row = matrix.length;
	//first find the transpose of the matrix.
        for (int i = 0; i < row; i++){
            for (int j = i; j < row; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
	//reverse each row
        for (int i = 0; i< row; i++){
            for(int j = 0; j< row/2; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][row - 1 - j];
                matrix[i][row - 1 - j] = temp;
            }
        }

    }

Below is the main method that will start the execution and print the resultant matrix.

public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        Matrix.rotateMatrix(matrix);
        for (int i = 0; i < matrix.length; i++){
            for (int j = 0; j < matrix.length; j++){
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

Explanation

Click to Suggest Your Own Explanation

matrix-rotation-iteration

Other Similar Java Programs:

1 Java Program to test if given number is Armstrong or not
2 Java Program to test if a given number is Fibonacci or not
3 java program to find distinct word list from a file
4 Java program to find duplicate character from a string
5 Java Program to find middle index of array where both ends sum is equal
6 Java Program to find line with max character length in descending order in Java
7 Java Program to find max two numbers in an array
8 Java program to find max repeated words from a file
9 Java program to find sum of prime numbers
10 Java program to reverse a given number
11 Java program to find permutations of a given string
12 Java program to reverse a given string
13 Java program to find factorial of a given number
14 Java Program for Binary Search
15 Java Program to Add Two 2D Matrix
16 3 Ways to Check if Given Words are Anagram or not
17 Java Program to Find LCM of a Two Given Number
18 Check Given String is Rotation of Another String
19 Java Program To Check If A Given Number is A Perfect Number
20 Remove Common Characters From Given Strings
21 Find Second Largest Number in Array
22 Java Program To Find the Longest Palindrome Present in a String
23 Java Program to Reverse an Array in Place Without Using Any Second Array
24 Java Program to Print 1 To 10 Without Using Loop
25 Write a Java Program to Compare Files in Java
26 Java Program to Find missing Number in an Array
27 Java Program to Find First non Repeated Character in a String
28 Write a Java Program to Find Union and Intersection of Arrays in Java
29 Writing a Java program to rotate an array by d elements.
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2020 Devglan. All rights reserved.