0
0
CProgramBeginner · 2 min read

C Program to Rotate Matrix 90 Degrees Clockwise

To rotate a square matrix 90 degrees clockwise in C, use nested loops to assign rotated[j][n-1-i] = original[i][j] for each element, where n is matrix size.
📋

Examples

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

How to Think About It

Think of the matrix as a grid of numbers. To rotate it 90 degrees clockwise, each element moves to a new position where the row becomes the column and the column becomes the reversed row. So, the element at row i and column j moves to row j and column n-1-i in the rotated matrix.
📐

Algorithm

1
Get the size n of the square matrix.
2
Create a new matrix to hold the rotated result.
3
For each row i from 0 to n-1:
4
For each column j from 0 to n-1:
5
Assign rotated[j][n-1-i] = original[i][j].
6
Print or return the rotated matrix.
💻

Code

c
#include <stdio.h>

int main() {
    int n = 3;
    int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    int rotated[3][3];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            rotated[j][n - 1 - i] = matrix[i][j];
        }
    }

    printf("Rotated matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", rotated[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Output
Rotated matrix: 7 4 1 8 5 2 9 6 3
🔍

Dry Run

Let's trace the 3x3 matrix [[1,2,3],[4,5,6],[7,8,9]] through the rotation code.

1

Start with original matrix

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

2

Assign rotated[j][n-1-i] = matrix[i][j]

For i=0, j=0: rotated[0][2] = 1 For i=0, j=1: rotated[1][2] = 2 For i=0, j=2: rotated[2][2] = 3 For i=1, j=0: rotated[0][1] = 4 For i=1, j=1: rotated[1][1] = 5 For i=1, j=2: rotated[2][1] = 6 For i=2, j=0: rotated[0][0] = 7 For i=2, j=1: rotated[1][0] = 8 For i=2, j=2: rotated[2][0] = 9

3

Resulting rotated matrix

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

i,jrotated[j][n-1-i]value assigned
0,0rotated[0][2]1
0,1rotated[1][2]2
0,2rotated[2][2]3
1,0rotated[0][1]4
1,1rotated[1][1]5
1,2rotated[2][1]6
2,0rotated[0][0]7
2,1rotated[1][0]8
2,2rotated[2][0]9
💡

Why This Works

Step 1: Mapping positions

Each element at position (i, j) in the original matrix moves to (j, n-1-i) in the rotated matrix, effectively swapping rows and columns with a reversed row index.

Step 2: Using nested loops

Two loops go through every element so we can assign each value to its new rotated position.

Step 3: Creating a new matrix

We use a separate matrix to store rotated values to avoid overwriting original data during the process.

🔄

Alternative Approaches

In-place rotation by transposing and reversing rows
c
#include <stdio.h>

int main() {
    int n = 3;
    int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

    // Transpose the matrix
    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;
        }
    }

    // Reverse each row
    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;
        }
    }

    printf("Rotated matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}
This method rotates the matrix in place without extra space but is slightly more complex.
Rotate 90 degrees counterclockwise
c
#include <stdio.h>

int main() {
    int n = 3;
    int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    int rotated[3][3];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            rotated[n - 1 - j][i] = matrix[i][j];
        }
    }

    printf("Rotated matrix (90 degrees CCW):\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", rotated[i][j]);
        }
        printf("\n");
    }
    return 0;
}
This rotates the matrix 90 degrees counterclockwise by changing the assignment indices.

Complexity: O(n^2) time, O(n^2) space

Time Complexity

The program uses two nested loops each running n times, so it processes n*n elements, resulting in O(n^2) time.

Space Complexity

The main approach uses an extra matrix of the same size to store rotated values, so space complexity is O(n^2). The in-place method reduces space to O(1).

Which Approach is Fastest?

Both methods run in O(n^2) time, but the in-place rotation saves memory at the cost of more complex code.

ApproachTimeSpaceBest For
Using extra matrixO(n^2)O(n^2)Simplicity and clarity
In-place transpose + reverseO(n^2)O(1)Memory efficiency and large matrices
Counterclockwise rotationO(n^2)O(n^2)Different rotation direction
💡
Use a separate matrix to avoid overwriting values when rotating a matrix.
⚠️
Beginners often confuse the row and column indices or forget to reverse the row index when assigning rotated positions.