# Matrix Rotation in C By 90 Degree | Clockwise & Anti-Clockwise

## Introduction

In this post, I am going to write a program for matrix rotation in c. In this program, we will rotate the matrix by 90 degrees to both sides i.e. clockwise and anti-clockwise. Before writing the c program for this, let us see the following figures.

### Matrix Rotation in C – 90 Degrees Clockwise

In the following diagram, we are rotating the 3*3 matrix by 90 degrees clockwise. You can see how this matrix will look like after rotating about 90 degrees clockwise. In simple words, we can say that we are rotating the matrix from left to right.

Similarly, we are going to rotate the same matrix by 90 degrees anticlockwise.

### Matrix Rotation in C – 90 Degrees Anti- Clockwise

Have you observed any change in both the diagrams? In the second figure, we are rotating the matrix to the left side from the right side i.e. anti-clockwise.

Now, I am going to tell you the logic of this program of matrix rotation in c.

### Logic

The logic of this program is so simple. We know that matrix is made up of rows and columns only.

1. When we rotate the original matrix by 90 degrees clockwise, then the first column of the original matrix becomes the first row of the output matrix and the last number of the first column of the original matrix becomes the first number of the first row of the matrix and so on.
2. When we rotate the original matrix by 90 degrees anti-clockwise, then the last column of the original matrix becomes the first row of the output matrix and the first number of the last column of the original matrix becomes the first number of the first row of the matrix and so on.

I recommend you to learn arrays in c programming for understanding this program.

Also Read: C Program to Remove Zeros from a number

Here, I have written two separate c programs for matrix rotation i.e. clockwise and anti-clockwise.

### Matrix Rotation in C By 90 Degree Clockwise

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
printf("Original Array\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Rotate Matrix by 90 degrees\n");
for(i=0;i<3;i++)
{
for(j=2;j>=0;j--)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}``````

See the output of this program.

### Matrix Rotation in C By 90 Degree Anti-Clockwise

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
printf("Original Array\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Rotate Matrix by 90 degrees\n");
for(i=2;i>=0;i--)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}``````

This program will rotate the matrix in an anti-clockwise direction by 90 degrees. See the following output.

I hope you have understood both these programs with logic. I have also made a video on the same topic. You can watch it here.

C Program to ROTATE Matrix by 90 De... x
C Program to ROTATE Matrix by 90 Degrees | Clockwise & Anti-Clockwise

If you have any difficulty regarding this program, then please feel free to contact me. Thank you.