# Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by 90 degrees in Java. The anti-clockwise rotations of the matrix is also referred as the left rotation of a matrix, while the clockwise rotation is indeed referred to as the right rotation of that same matrix.

## What is a Matrix in Java?

Technically referring, a matrix is a rectangular, two-dimensional array that contains either integers, symbols, or statements as its fundamental elements. The elements in the matrix are arranged in rows and columns.

For an instance, m x n matrix refers to a matrix containing 'm' number of rows and 'n' number of columns. 'a [i][j]', which denotes that the element an is represented in the i-th row and j-th column, can be used to represent individual values in the matrix, which are referred to as elements.

Look at the example below to understand how a matrix actually looks like

Example:

a[][]= { 12, 5, 0,

11, 7, 6,

10, 8, 15}

The above example is a matrix or it can also be termed as two-dimensional array, with 3 rows and 3 columns. The representation of the above matrix elements in Z form shall certainly include the entire first row of the matrix, the right diagonal and the entire last row of the matrix too.

Now let us discuss about how to rotate a given matrix by 90 degrees in clockwise direction and also in anti-clockwise direction.

Let us first look at how a matrix can be rotated in clockwise rotation or simply right rotation.

## Rotating matrix 90 degrees in clockwise direction.

Rotating a matrix 90 degrees in clockwise direction is process which can be accomplished by implementing two steps. The two steps are:

• Initially, determine the matrix's transpose.
• Unless the matrix is 3x3, swap the entries in the initial column with those in the last column. The second column does not change.

Note: The number of columns and rows in a matrix must and should be same. Then only the rotation of any matrix in any direction would be possible.

Look at the example below to understand how the rotation works.

{[1 ,2 ,3]

[4 ,5 ,6]

[7 ,8 ,9]}

Let us find the transpose of the above matrix.

{[1 ,4 ,7]

[2 ,5 ,8]

[3 ,6 ,9]}

Now lets us swap the first column and the last column.

{[7 ,4 ,1]

[8 ,5 ,2]

[9 ,6 ,3]}

The above matrix after rotating 90 degrees in clockwise direction. Lets us go through a simple example program.

File name: Clockwise.java

``````public class Clockwise
{
//method to rotate the matrix 90 degrees in clockwise direction
static void rotateRight(int matrix[][],int n)
{
//to find out the transpose of the matrix
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}

//reversing the elements of each row
for(int i=0;i<n;i++)
{
int low = 0, high = n-1;
while(low < high)
{
int temp = matrix[i][low];
matrix[i][low] = matrix[i][high];
matrix[i][high] = temp;
low++;
high--;
}
}

System.out.println("The matrix after rotating 90 degrees in clockwise direction: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(matrix[i][j]+" " +"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
int n=3;
// creating a matrix
int matrix[][] = new int[][]{ {1,2,3}, {4,5,6} , {7,8,9} };
System.out.println("The matrix before rotating 90 degrees in clockwise direction: ");
// displays the matrix
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//displays the elements of the matrix
System.out.print(matrix[i][j]+" " +"\t");
}
System.out.println();
}
//invoking the method
rotateRight(matrix, n);
}
}
``````

Output

``````The matrix before rotating 90 degrees in clockwise direction:
1       2       3
4       5       6
7       8       9
The matrix after rotating 90 degrees in clockwise direction:
7       4       1
8       5       2
9       6       3
``````

## Rotating matrix 90 degrees in anti-clockwise direction

File name: Anticlockwise.java

``````public class Anticlockwise
{
//method to rotate the matrix 90 degrees in anti-clockwise direction
static void rotateLeft(int matrix[][], int n)
{
//finding the transpose of the matrix
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i=0;i<n;i++)
{
int low = 0;
int high = n-1;
while(low < high)
{
int temp = matrix[low][i];
matrix[low][i] = matrix[high][i];
matrix[high][i] = temp;
low++;
high--;
}
}
//displays the matrix after the rotation
System.out.println("The matrix after rotating in in anti-clockwise direction: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(matrix[i][j]+" "+"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
int n=3;
//creating a matrix
int matrix[][] = new int[][]{ {1,2,3}, {4,5,6} , {7,8,9} };
//displays the matrix
System.out.println("The matrix before rotating 90 degrees in anti-clockwise direction: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//displays elements of the matrix
System.out.print(matrix[i][j]+" "+"\t");
}
System.out.println();
}
rotateLeft(matrix,n);
}
}
``````

Output:

``````The matrix before rotating 90 degrees in anti-clockwise direction:
1       2       3
4       5       6
7       8       9
The matrix after rotating in in anti-clockwise direction:
3       6       9
2       5       8
1       4       7
``````