Program to Rotate a matrix by 90 degrees in the clockwise direction in C

C


In this article, we are given an N x N matrix, and our task is to rotate it by 90 degrees clockwise in C.

Example

Input: [1 2 3], [4 5 6], [7 8 9] Output: [7 4 1], [8 5 2], [9 6 3] 

Below are different approaches to rotate a matrix by 90 degrees in the clockwise direction:

Using Brute Force Approach

This is the simple and direct approach to understand and implement. In this approach, we create a new matrix and manually place each element at its correct rotated position.

  • Define a function and take the given matrix and its size as input parameters.
  • Now, create a new matrix of the same size.Iterate through the elements and place them in their new correct rotated positions.
  • Copy the rotated matrix back to the original matrix. Return the rotated matrix.

Example

Here is an example code implementing above steps to rotate a matrix by 90 degrees in the clockwise direction using brute force approach.

#include <stdio.h> #define N 3 void rotateMatrix(int matrix[N][N]) { int rotated[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { rotated[j][N - 1 - i] = matrix[i][j]; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { matrix[i][j] = rotated[i][j]; } } } void printMatrix(int matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int matrix[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printf("Original Matrix: \n"); printMatrix(matrix); rotateMatrix(matrix); printf("Rotated Matrix: \n"); printMatrix(matrix); return 0; } 

Using In-Place Rotation

This is a space-optimized approach. We make modifications in the input matrix without taking a new matrix. In this approach, we will rotate the matrix in place by first transposing the matrix and then reversing the row.

  • Define a function and take the matrix and its size as input.
  • Now, transpose the matrix (Transposing of the matrix is converting the rows into columns and vice-versa.)
  • Reverse each row to obtain the required rotated matrix. Print the rotated Matrix.

Example

The following example code uses in-place rotation to rotate a matrix by 90 degrees in the clockwise direction.

#include <stdio.h> #define N 3 void rotateMatrixInPlace(int matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = i + 1; 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++) { for (int j = 0; j < N / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][N - 1 - j]; matrix[i][N - 1 - j] = temp; } } } void printMatrix(int matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int matrix[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printf("Original Matrix: \n"); printMatrix(matrix); rotateMatrixInPlace(matrix); printf("Rotated Matrix: \n"); printMatrix(matrix); return 0; } 

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n^2) O(n^2)
In-Place Rotation O(n^2) O(1)
Updated on: 2025-04-17T13:29:28+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements