# C Program to find the transpose of a matrix

Last updated on September 23, 2020

The following is a C program to find the transpose of a matrix:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | ```
/********************************************
* Program to find the transpose of a matrix
********************************************/
#include<stdio.h> // include stdio.h
#define ROW 2
#define COL 4
int main()
{
int i, j, mat[ROW][COL], trans_mat[COL][ROW];
printf("Enter matrix: \n");
// input matrix
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
scanf("%d", &mat[i][j]);
}
}
/* create transpose matrix by
* switch entries
*/
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
trans_mat[j][i] = mat[i][j];
}
}
printf("\nTranspose matrix: \n");
// print transpose matrix
for(i = 0; i < COL; i++)
{
for(j = 0; j < ROW; j++)
{
printf("%d ", trans_mat[i][j]);
}
printf("\n");
}
// signal to operating system everything works fine
return 0;
}
``` |

**Expected Output:**

1 2 3 4 5 6 7 8 9 | ```
Enter matrix:
1 2 3 4
5 6 7 8
Transpose matrix:
1 5
2 6
3 7
4 8
``` |

## How it works #

Let `A`

be a matrix of size `m x n`

, then the matrix obtained by interchanging the rows and columns is called the Transpose of `A`

.

The transpose of a matrix is denoted by \(A^T\) . For example:

\[

A = \left(\begin{array}{cc}1 & 2\\3 & 4\\5 & 6\end{array}\right)

\]

then

\[

A^T = \left(\begin{array}{ccc}1 & 3 & 5\\2 & 4 & 6\end{array}\right)

\]

Here is how the program works:

- The first for loop (lines 16-22) asks the user to input the matrix.
- The second for loop (lines 27-33) creates the transpose matrix by interchanging rows with columns.
- The third for loop (lines 38-46) prints the transpose of a matrix.

**Recommended Reading:**

- C Program to reverse the elements of an array
- C Program to sum the elements of an array
- C Program to find the count of even and odd elements in the array
- C Program to add two Matrices
- C Program to multiply two matrices

Load Comments