Array Exercises
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. Logic to find transpose of a matrix in C programming.
Required knowledge
Basic C programming, C for loop, ArrayS
Transpose of a matrix
Transpose of a matrix A is defined as converting all rows into columns and columns into rows. Transpose of matrix A is written as AT.

Transpose of a matrix A is defined as -
ATij = Aji ; Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
Logic to find transpose of a matrix
Below is the step by step descriptive logic to find transpose of a matrix.
- 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 MAX_ROWS to iterate through rows. The loop structure should look like for(row=0; row<MAX_ROWS; row++).
- To iterate through each column of the matrix, run an inner loop from 0 to MAX_COLS. The loop structure must look like for(col=0; col<MAX_COLS; 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].
Program to find transpose of a matrix
/** * C program to find transpose of a matrix */ #include <stdio.h> #define MAX_ROWS 3 #define MAX_COLS 3 int main() { int A[MAX_ROWS][MAX_COLS]; // Original matrix int B[MAX_COLS][MAX_ROWS]; // Transpose matrix int row, col; /* Input elements in matrix A from user */ printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS); for(row=0; row<MAX_ROWS; row++) { for(col=0; col<MAX_COLS; col++) { scanf("%d", &A[row][col]); } } /* * Find transpose of matrix A */ for(row=0; row<MAX_ROWS; row++) { for(col=0; col<MAX_COLS; 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<MAX_ROWS; row++) { for(col=0; col<MAX_COLS; col++) { printf("%d ", A[row][col]); } printf("\n"); } /* Print the transpose of matrix A */ printf("Transpose of matrix A: \n"); for(row=0; row<MAX_COLS; row++) { for(col=0; col<MAX_ROWS; col++) { printf("%d ", B[row][col]); } printf("\n"); } return 0; }
Output:
Enter elements in matrix of size 3x3:
10 20 30
40 50 60
70 80 90
Original matrix:
10 20 30
40 50 60
70 80 90
Transpose of matrix A:
10 40 70
20 50 80
30 60 90
Process returned 0 (0x0) execution time : 27.624 s
Program to find transpose of a matrix
/** * C program to find transpose of a matrix */ #includeint main() { int A[10][10]; // Original matrix int B[10][10]; // Transpose matrix int row, col,m,n; printf("Enter number of rows:"); scanf("%d",&m); printf("Enter number of columns:"); scanf("%d",&n); /* Input elements in matrix A from user */ printf("Enter elements in matrix of size %dx%d: \n", m, n); for(row=0; row<m; row++) { for(col=0; col<n; col++) { scanf("%d", &A[row][col]); } } /* * Find transpose of matrix A */ for(row=0; row<m; row++) { for(col=0; col<n; 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<m; row++) { for(col=0; col<n; col++) { printf("%d ", A[row][col]); } printf("\n"); } /* Print the transpose of matrix A */ printf("Transpose of matrix A: \n"); for(row=0; row<m; row++) { for(col=0; col<n; col++) { printf("%d ", B[row][col]); } printf("\n"); } return 0; }