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.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

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.

matrix rotation in c clockwise
Matrix Rotation in C 90 Degrees Clockwise

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

Matrix Rotation in C – 90 Degrees Anti- Clockwise

Matrix Rotation in C 90 Degrees Anti Clockwise
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.

Also Read: C Program to Print Multiples of 5 using do while loop

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 90 Degrees Clockwise Output

Also Read: Reverse a Number using getchar and putchar function in c

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;
}

Also Read: C Program to Print Numbers Except Multiples of n

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

Matrix Rotation in C 90 Degrees Anti Clockwise 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...
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.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. C Program to Remove White Spaces and Comments from a File

Leave a Comment