C++ Program to Rotate Matrix Elements

Last Updated : 21 Aug, 2026

Rotating matrix elements means shifting the elements of each layer or ring of a matrix in a clockwise direction. The rotation is performed layer by layer, starting from the outermost ring and moving toward the inner rings.

  • Each ring is rotated independently in a clockwise direction.
  • The matrix is modified in-place without using an additional matrix.

Examples:

Input:

1 2 3
4 5 6
7 8 9


Output:

4 1 2
7 5 3
8 9 6

The outer ring elements are rotated clockwise, while the center element remains unchanged.

For 4*4 matrix:
Input:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16


Output:

5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

Approach

The matrix can be viewed as a set of concentric rings. We rotate each ring one at a time, starting from the outermost ring.

  • Maintain four boundaries: row, col, m, and n.
  • row and col represent the starting row and column of the current ring.
  • m and n represent the ending row and column of the current ring.
  • Store the first element of the next row in a temporary variable.
  • Move the elements of the top row one position to the right.
  • Move the elements of the last column one position downward.
  • Move the elements of the bottom row one position to the left.
  • Move the elements of the first column one position upward.
  • Move the boundaries inward and repeat the process for the next ring.
C++
#include <iostream>
using namespace std;

const int MAX = 100;

// Rotates all rings of the matrix clockwise by one position
void rotateMatrix(int mat[][MAX], int rows, int cols)
{
    int top = 0;
    int left = 0;
    int bottom = rows;
    int right = cols;

    while (top < bottom && left < right) {

        // A ring must contain at least two rows
        // and two columns to be rotated.
        if (top + 1 == bottom ||
            left + 1 == right) {
            break;
        }

        // Store the first element of the
        // second row.
        int prev = mat[top + 1][left];

        // Move the top row one position right.
        for (int j = left; j < right; j++) {
            int curr = mat[top][j];
            mat[top][j] = prev;
            prev = curr;
        }
        top++;

        // Move the right column one position down.
        for (int i = top; i < bottom; i++) {
            int curr = mat[i][right - 1];
            mat[i][right - 1] = prev;
            prev = curr;
        }
        right--;

        // Move the bottom row one position left.
        if (top < bottom) {
            for (int j = right - 1; j >= left; j--) {
                int curr = mat[bottom - 1][j];
                mat[bottom - 1][j] = prev;
                prev = curr;
            }
        }
        bottom--;

        // Move the left column one position up.
        if (left < right) {
            for (int i = bottom - 1; i >= top; i--) {
                int curr = mat[i][left];
                mat[i][left] = prev;
                prev = curr;
            }
        }
        left++;
    }
}

int main()
{
    int mat[4][MAX] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };

    int rows = 4;
    int cols = 4;

    rotateMatrix(mat, rows, cols);

    cout << "Matrix after clockwise rotation:\n";

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << mat[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
} 

Output
Matrix after clockwise rotation:
5 1 2 3 
9 10 6 4 
13 11 7 8 
14 15 16 12 

Explanation

  • top, bottom, left, and right define the current ring boundaries.
  • prev temporarily stores elements during rotation.
  • Four loops shift the top row, right column, bottom row, and left column.
  • The boundaries move inward to rotate the next ring.
  • The matrix is modified in-place, so no extra matrix is required.
Comment