Matrix rotation by 90 degrees clockwise and anticlockwise | Faceprep

20 min read

Matrix rotation by 90 degrees can be done in 

  • clockwise
  • anticlockwise

 

Matrix rotation by 90 degrees - clockwise

To rotate a matrix clockwise by 90 degrees,

  • Find the transpose of the matrix.
  • Reverse every rows of the  matrix.

 

matrix rotation by 90 degrees

In case of anti-clockwise rotation, we find the transpose of the matrix and reverse every columns of the transposed matrix.

 

C++ program to rotate matrix by 90 degrees clockwise

#include <bits/stdc++.h>
#define N 3
using namespace std;

// Function to reverse rows of the matrix

void reverse_rows(int arr[N][N])
{
int k;
for (int i = 0; i < N; i++)
{
k = N-1;
for (int j = 0; j < k; j++)
{
swap(arr[i][j], arr[i][k]);
k--;
}}}

// Transpose of a matrix
void transpose(int arr[N][N])
{
for (int i = 0; i < N; i++)
for (int j = i; j < N; j++)
swap(arr[i][j], arr[j][i]);
}

// print the matrix
void print_matrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf("%d\t", mat[i][j]);

printf("\n");
}
printf("\n");
}

// Main function
int main()
{
int mat[N][N] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout<<"The matrix before rotation\n";
print_matrix(mat);
transpose(mat);
cout<<"The matrix after rotation - Clockwise\n";
reverse_rows(mat);
print_matrix(mat);
return 0;
}


 

OUTPUT


matrix rotation by 90 degrees clockwise

 

Java program to rotate a matrix by 90 degrees clockwise

public class Main
{

// Function to reverse rows of the matrix
public static void reverse_rows(int mat[][])
{
int k;
for (int i = 0; i < mat.length; i++)
{
k = mat.length-1;
for (int j = 0; j < k; j++)
{
int temp = mat[i][j];
mat[i][j] = mat[i][k];
mat[i][k] = temp;
k--;
}}}

// Function to print the matrix
public static void print_matrix(int mat[][])
{
int i,j;
for(i=0;i<mat.length;i++)
{
for(j=0;j<mat.length;j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.print("\n");
}}

// Function to transpose the matrix
public static void transpose_matrix(int mat[][])
{
int i,j;
for(i=0;i<mat.length;i++)
{
for(j=i;j<mat.length;j++)
{
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}}}

// Main function
public static void main(String[] args)
{
int mat[][] = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println("The matrix before rotation ");
print_matrix(mat);
System.out.println("Transpose of the matrix ");
transpose_matrix(mat);
System.out.println("The matrix after rotation ");
reverse_rows(mat);
print_matrix(mat);
}}

 

Python program to rotate a matrix by 90 degrees clockwise

# Function to print the matrix
def print_matrix(mat):
for i in range (len(mat)):
for j in range (len(mat)):
print(str(mat[i][j]), end = " ")
print()

# Function to transpose the matrix
def transpose_matrix(mat):
for i in range(len(mat)):
for j in range(i,len(mat)):
mat[i][j], mat[j][i] = mat[j][i],mat[i][j]

# Function to reverse the rows of the matrix
def reverse_rows(mat):
for i in range (len(mat)):
k = len(mat) - 1;
for j in range(0,k):
mat[i][j], mat[i][k] = mat[i][k], mat[i][j]
k = k - 1


# Main function
mat = [[1,2,3],
[4,5,6],
[7,8,9],
];
print("The array before rotation is ")
print_matrix(mat)
transpose_matrix(mat)
reverse_rows(mat)
print("\nThe array after rotation is ")
print_matrix(mat)

 

Matrix rotation by 90 degrees - anticlockwise

To rotate a matrix anticlockwise by 90 degrees,

  • Find the transpose of the matrix.
  • Reverse every columns of the matrix.

 

C++ program to rotate matrix by 90 degrees anticlockwise

#include <bits/stdc++.h>
#define N 3
using namespace std;

//Function to reverse the columns of the matrix
void reverse_column(int arr[N][N])
{
int k;
for (int i = 0; i < N; i++)
{
k = N-1;
for (int j = 0; j < k; j++)
{
swap(arr[j][i], arr[k][i]);
k--;
}}}

// Transpose of a matrix
void transpose(int arr[N][N])
{
for (int i = 0; i < N; i++)
for (int j = i; j < N; j++)
swap(arr[i][j], arr[j][i]);
}

// print the matrix
void print_matrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf("%d\t", mat[i][j]);

printf("\n");
}
printf("\n");
}

// Main function
int main()
{
int mat[N][N] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout<<"The matrix before rotation\n";
print_matrix(mat);
transpose(mat);
cout<<"The matrix after rotation - anticlockwise\n";
reverse_column(mat);
print_matrix(mat);
return 0;
}

 

OUTPUT

matrix-rotation-90-degrees-anticlockwise

 

Java program to rotate a matrix by 90 degrees anticlockwise

public class Main
{
// Function to reverse the columns of the matrix
public static void reverse_columns(int mat[][])
{
int k;
for (int i = 0; i < mat.length; i++)
{
k = mat.length - 1;
for (int j = 0; j < k; j++)
{
int temp = mat[j][i];
mat[j][i] = mat[k][i];
mat[k][i] = temp;
k--;
}}}

// Function to print the matrix
public static void print_matrix(int mat[][])
{
int i,j;
for(i=0;i<mat.length;i++)
{
for(j=0;j<mat.length;j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.print("\n");
}}

// Function to transpose the matrix
public static void transpose_matrix(int mat[][])
{
int i,j;
for(i=0;i<mat.length;i++)
{
for(j=i;j<mat.length;j++)
{
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}}}

// Main function
public static void main(String[] args)
{
int mat[][] = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println("The matrix before rotation ");
print_matrix(mat);
System.out.println("Transpose of the matrix ");
transpose_matrix(mat);
System.out.println("The matrix after rotation ");
reverse_columns(mat);
print_matrix(mat);
}}

 

Python program to rotate a matrix by 90 degrees anticlockwise

# Function to print the matrix

def print_matrix(mat):
for i in range (len(mat)):
for j in range (len(mat)):
print(str(mat[i][j]), end = " ")
print()

# Function to transpose the matrix
def transpose_matrix(mat):
for i in range(len(mat)):
for j in range(i,len(mat)):
mat[i][j], mat[j][i] = mat[j][i],mat[i][j]

# Function to reverse the columns of the matrix
def reverse_columns(mat):
for i in range (len(mat)):
k = len(mat) - 1;
for j in range(0,k):
mat[j][i], mat[k][i] = mat[k][i], mat[j][i]
k = k - 1

# Main Function
mat = [[1,2,3],
[4,5,6],
[7,8,9],
];
print("The array before rotation is ")
print_matrix(mat)
transpose_matrix(mat)
reverse_columns(mat)
print("\nThe array after rotation is ")
print_matrix(mat)
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error