Transpose of a Matrix in C

Last Updated : 1 Nov, 2025

Transpose of a matrix is a matrix that is obtained by swapping the rows and columns of the given matrix or vice versa, i.e., for the given matrix the elements in rows are interchanged with the elements in columns. For any given matrix A its transpose is denoted as At, or AT.

Note: The transpose of an m × n matrix will result in an n × m matrix.

transpose

Transpose of a Square Matrix

Square matrices are matrices that have an equal number of rows and columns. For any square matrix An×n, its transpose has the same order i.e., the transpose of A, At has order n × n. The rows and columns are interchanged in the transpose of a square matrix.

C
#include <stdio.h>

int main() {
    int n = 3;  

    int a[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Transpose in-place
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int temp = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = temp;
        }
    }

    // Display transpose
    printf("Transpose of the matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output
Transpose of the matrix:
1 4 7 
2 5 8 
3 6 9 

Complexity Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space: O(n 2)

Transpose of a Rectangular Matrix

The transpose of a rectangular matrix is formed by turning its rows into columns (and columns into rows).

If the original matrix is of size m × n, its transpose will be of size n × m. In other words, the element at position [i][j] moves to [j][i] in the transposed matrix.

C
#include <stdio.h>

int main()
{
    int m = 2, n = 3;

    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

    int t[3][2];

    // Find transpose
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            t[j][i] = a[i][j];
        }
    }

    // Display transpose
    printf("Transpose of the matrix:\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%d ", t[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output
Transpose of the matrix:
1 4 
2 5 
3 6 

Complexity Analysis

  • Time Complexity: O(n*m)
  • Auxiliary Space: O(n*m)
Comment