Finding transpose matrix in C

In this guide we will write a program to determine the transpose matrix. To determine transpose matrix what we have to do is just interchange its row to column and column to row. Consider the given example.

If a matrix A is,

4   5   6

7   8   9

2   3   4

Then the transpose matrix of A will be as follows;

4   7   2

5   8   3

6   9   4

We can write code for this program using different way. You can use function, recursion etc to obtain the transpose matrix. Let’s see a program bellow which will give the transpose matrix of given matrix as output.

We recommend you to see our 2D array in C chapter if you have not a clear idea about matrix or two dimensional array.

C program to get transpose matrix

// transpose matrix program in c

#include <stdio.h>

int main(){
  int main_matrix[20][20], trans_matrix[20][20], num1, num2, j, k;

  printf("Enter row and column number for each matrix : ");
  scanf("%d%d", &num1, &num2);

  printf("\nEnter all the elements of matrix here : \n");

  for(j = 0; j < num1; j++){

    for(k = 0; k < num2; k++){
      scanf("%d", &main_matrix[j][k]);
    }
  }

  for(j = 0; j < num1; j++){

    for(k = 0; k < num2; k++){
      trans_matrix[k][j] = main_matrix[j][k];
    }
  }

  printf("\nTranspose matrix of given matrix is : \n");

  for(j = 0; j < num2; j++) {

    for(k = 0; k < num1; k++){
      printf("%d ", trans_matrix[j][k]);
    }

    printf("\n");
  }

  return 0;
}

Output of transpose matrix program:

transpose-matrix-in-c-c-program-to-find-transpose-matrix

Now, you can try this program using function in C. One thing you may have noticed that the row number and column number will exchange in case of transpose matrix which we can not understand here because we have used a square matrix here.

Previous pageAdd two matrix

Next pagePrint string