C Programming Code Examples C > Arrays and Matrices Code Examples C program code to find transpose of a matrix C program code to find transpose of a matrix Write a C program to read elements in a matrix and find transpose of the given matrix. How to find transpose of a given matrix in C. Input elements in matrix A from user. Declare another matrix of same size as of A, to store transpose of matrix say B. To iterate through each element of matrix run two loops. Run an outer loop from 0 to maxrows to iterate through rows. The loop structure should look like for(row=0; row<maxrows; row++). To iterate through each column of the matrix, run an inner loop from 0 to maxcols. The loop structure must look like for(col=0; col<maxcols; col++). Inside inner loop we will perform actual transpose of the matrix. As per definition column of transpose matrix is equal to row of original matrix and vice versa. Means assign B[col][row] = A[row][col]. #include <stdio.h> #define maxrows 3 #define maxcols 3 int main() { int A[maxrows][maxcols]; // Original matrix int B[maxcols][maxrows]; // Transpose matrix int row, col; /* Input elements in matrix A from user */ printf("Enter elements in matrix of size %dx%d: \n", maxrows, maxcols); for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { scanf("%d", &A[row][col]); } } /* Find transpose of matrix A */ for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { /* Store each row of matrix A to each column of matrix B */ B[col][row] = A[row][col]; } } /* Print the original matrix A */ printf("\nOriginal matrix: \n"); for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { printf("%d ", A[row][col]); } printf("\n"); } /* Print the transpose of matrix A */ printf("Transpose of matrix A: \n"); for(row=0; row<maxcols; row++) { for(col=0; col<maxrows; col++) { printf("%d ", B[row][col]); } printf("\n"); } return 0; }