C Program to read & find the transpose of a matrix

May 5, 2022, Learn eTutorial

What is a transpose of a matrix?

In this c program, we need to calculate the transpose of the input matrix. Transpose of a matrix means we need to change all the elements in a row to column and all elements in the column to row. Transpose was introduced in 1858 by Clayey.

For example, let us assume a matrix of [1 2 3], and when we calculate the transpose of the matrix, it should be like this.



This means we have to interchange the Rows and Columns.

How to find the transpose of a matrix?

Here, we accept a matrix from the user. We change the order of the elements. We change the rows into columns and columns into rows. Finally, we display the matrix that we input and the output matrix separately in the matrix format.

The logic of this program is first to read the order of the matrix from the user. Accept the elements from the user and add that to the matrix using nested for loop. Display the matrix after that will find out the transpose of the matrix 'b' by changing the element position of the matrix  'A.' "A[j][i] = B[i[j]." After that, we display the Transpose Matrix using a nested for loop.


STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Initialize and Define the Matrix and the variables to use in the C program.

STEP 3: Accept the Order of the Matrix A using printf and scanf functions in C programming.

STEP 4: Accept the elements of Matrix A using nested for loop and scanf function.

STEP 5: Display the Matrix A using nested For loop and printf function.

STEP 6: Find out the Transpose of the Matrix by using nested for loop and place B[i][j]=A[j][i] where 'B' is the transpose matrix.

STEP 7: Display the Transpose Matrix 'B' using nested for loop and printf function in C.

C Source Code

                                          #include <stdio.h>

void main()
  int i, j, M, N;
  int A[10][10], B[10][10];

  printf("Enter the order of matrix A\n");
  scanf("%d %d", & M, & N);
  printf("Enter the elements of matrix\n"); /* enters the order of matrix with the values for each position of the matrix  */
   for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      scanf("%d", & A[i][j]);
   printf("The given matrix is \n"); /* just prints the matrix as such */
  for (i = 0; i < M; ++i) {
    for (j = 0; j < N; ++j) {
      printf(" %d", A[i][j]);

    /* Finding Transpose of matrix*/

    for (i = 0; i < M; i++) {
      for (j = 0; j < N; j++) {
        B[i][j] = A[j][i]; /* changing the elements of A matrix as per we discussed above */
    printf("Its Transpose is\n");
    for (i = 0; i < M; i++) {
      for (j = 0; j < N; j++) {
        printf(" %d", B[i][j]); /* finally printing the result matrix */
  } /*End of main()*/


Enter the order of matrix A
3 3

Enter the elements of matrix

Matrix A is
1  2  3
4  5  6
7  8  9

Its Transpose is
1  4  7
2  5  8
3  6  9