# Rotate a Matrix in C++ ( Clockwise and Anticlockwise )

In this tutorial, we are going to learn how to rotate a  matrix in C++ in both clockwise and anticlockwise direction by 90 degrees. Let us understand what we need to do to solve this particular problem.

## Explanation for Clockwise rotation:

A given N x N matrix will have (N/2) square cycles. Like a 3 X 3 matrix will have 1 cycle. The cycle is formed by its first row, last column, last row, and last column.
For each square cycle, we are going to swap the elements that are involved with the corresponding cell in the matrix in the clockwise direction. We will just need a temporary variable for keeping that array element.

Example:

Input:

```1 2 3

4 5 6

7 8 9```

Output:

```7 4 1

8 5 2

9 6 3```

## Rotate a matrix by 90 degrees in Clockwise direction:

Source code in c++:

```// Rotating matrix by 90 degree in Clockwise direction in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter size of matrix (NxN): ";
cin>>n;
int arr[n][n];
cout<<"\nEnter matrix elements:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
for (int i=0;i<n/2;i++)
{
for (int j=i;j<n-i-1;j++)
{
// Swapping elements after each iteration in Clockwise direction
int temp=arr[i][j];
arr[i][j]=arr[n-1-j][i];
arr[n-1-j][i]=arr[n-1-i][n-1-j];
arr[n-1-i][n-1-j]=arr[j][n-1-i];
arr[j][n-1-i]=temp;
}
}

// Printing matrix elements after rotation
cout<<"\nMatrix after rotating 90 degree clockwise:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}```

Input:

```Enter size of matrix (NxN): 4

Enter matrix elements:
5 10 15 20
25 30 35 40
45 50 55 60
65 70 75 80```

Output:

Below is the output of our code:

```Matrix after rotating 90 degree clockwise:
65 45 25 5
70 50 30 10
75 55 35 15
80 60 40 20```

## Explanation for Anticlockwise rotation:

A given N x N matrix will have (N/2) square cycles. Like a 3 X 3 matrix will have 1 cycle. The cycle is formed by its first row, last column, last row, and first column.
For each square cycle, we are going to swap the elements involved with the corresponding cell in the matrix in the anticlockwise direction in this case. We will just need a temporary variable for keeping that array element.

Example:

Input:

```1 2 3

4 5 6

7 8 9```

Output:

```3 6 9

2 5 8

1 4 7```

## Rotate a matrix by 90 degrees in Anticlockwise direction:

Source code in c++:

```// Rotating matrix by 90 degree in Anticlockwise direction in C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter size of matrix (NxN): ";
cin>>n;
int arr[n][n];
cout<<"\nEnter matrix elements:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
for(int i=0;i<n/2;i++)
{
for(int j=i;j<n-i-1;j++)
{
// Swapping elements after each iteration in Anticlockwise direction
int temp=arr[i][j];
arr[i][j]=arr[j][n-i-1];
arr[j][n-i-1]=arr[n-i-1][n-j-1];
arr[n-i-1][n-j-1]=arr[n-j-1][i];
arr[n-j-1][i]=temp;
}
}

// Printing matrix elements after rotation
cout<<"\nMatrix after rotating 90 degree anticlockwise:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}```

Input:

```Enter size of matrix (NxN): 4

Enter matrix elements:
5 10 15 20
25 30 35 40
45 50 55 60
65 70 75 80```

Output:

```Matrix after rotating 90 degree anticlockwise:
20 40 60 80
15 35 55 75
10 30 50 70
5 25 45 65```

You can also learn:

Print a given matrix in counter-clockwise spiral form in c++

How to find a Transpose of a matrix by using java

Do not forget to comment if you find anything wrong in the post or you want to share some information regarding the same.