You are here

Transpose of a matrix in C

Transpose of a matrix in C language: This C program prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. For example, consider the following 3 X 2 matrix:
1 2
3 4
5 6
Transpose of the matrix:
1 3 5
2 4 6
When we transpose a matrix then its order changes, but for a square matrix it remains the same.

C program to find transpose of a matrix

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int m, n, c, d, matrix[10][10], transpose[10][10];
  6.  
  7.    printf("Enter the number of rows and columns of matrix\n");
  8.    scanf("%d%d", &m, &n);
  9.    
  10.    printf("Enter elements of the matrix\n");
  11.  
  12.    for (c = 0; c < m; c++)
  13.       for(d = 0; d < n; d++)
  14.          scanf("%d", &matrix[c][d]);
  15.  
  16.    for (c = 0; c < m; c++)
  17.       for( d = 0 ; d < n ; d++ )
  18.          transpose[d][c] = matrix[c][d];
  19.  
  20.    printf("Transpose of the matrix:\n");
  21.  
  22.    for (c = 0; c < n; c++) {
  23.       for (d = 0; d < m; d++)
  24.          printf("%d\t", transpose[c][d]);
  25.       printf("\n");
  26.    }
  27.  
  28.    return 0;
  29. }

Output of program:
Transpose matrix C program output

Download Transpose Matrix program.