# Write a program to make transformation matrix rotation 90 degrees clockwise in c language

Write a program to make transformation matrix rotation 90 degrees clockwise in __c language__. In this program, you will learn how to transform a matrix 90 degrees clockwise.

**Sample of matrix rotation 90 degrees clockwise:**

x1 y1 z1

x2 y2 z2

x3 y3 z3

**After rotation the matrix 90 degrees clockwise:**

x3 x2 x1

y3 y2 y1

z3 z2 z1

Take an example to complete out this approach through a __c program__:

```
// Write a program to make transformation matrix rotation 90 degrees clockwise in c language
#include <stdio.h>
#define N 4
void rotate90DegClockwise(int x[N][N]);
void printMatrix(int x[N][N]);
int main() {
int arru[N][N] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
printf("-----The input matrix before 90 degrees clock-wise rotation-----\n\n");
printMatrix(arru);
// this function will be rotate each element clock-wise
rotate90DegClockwise(arru);
printf("\n-----The matrix after 90 degrees clock-wise rotation-----\n\n");
printMatrix(arru);
return 0;
}
// It's the function to rotate the matrix 90 degree clock-wise
void rotate90DegClockwise(int a[N][N]) {
// It will traverse the each cycle
for (int i = 0; i < N / 2; i++) {
for (int j = i; j < N - i - 1; j++) {
// It will swap elements of each cycle in clock-wise direction
int temp = a[i][j];
a[i][j] = a[N - 1 - j][i];
a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];
a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];
a[j][N - 1 - i] = temp;
}
}
}
// It's the function for print the rotated matrix
void printMatrix(int arr[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf("%d\t", arr[i][j]);
printf("\n\n");
}
}
```

**Output:**

-----The input matrix before 90 degrees clock-wise rotation-----

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

-----The matrix after 90 degrees clock-wise rotation-----

13 9 5 1

14 10 6 2

15 11 7 3

16 12 8 4

## Tags:

# write a c program rotate matrix 90 degrees anti-clockwise

# write a c program rotate matrix by 90 degrees clock-wise

# write a c program to rotate a matrix by 180 degrees