Algorithm to Find the Transpose of a Matrix
1.Declare a 2-dimensional array and initialize it.
2.Make entries of the matrix from the user.
3.Declare another 2-dimensional array.
4.Interchange the rows and columns and store them in the second(transpose matrix) array.
5.Print the transpose matrix.

C Program to Find the Transpose of a Square Matrix
#include<stdio.h>
int main(){
int i=3,j=3;
int rows, columns;
int arr[3][3]={{6,1,3},{4,0,3},{3,0,5}};
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
b[columns][rows] = a[rows][columns];
}
}
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d \t ", b[rows][columns]);
}
printf("\n");
}
return 0;
}
Output
6 4 3
1 0 0
3 3 5
Also see, Tribonacci Series and Short int in C Programming
C Program to find Transpose of a Rectangular Matrix
To find the transpose of a rectangular matrix, we need to interchange its rows and columns. In other words, the element at the i-th row and j-th column in the original matrix becomes the element at the j-th row and i-th column in the transposed matrix.
Let's see a step-by-step explanation of the C program to find the transpose of a rectangular matrix:
1. Declare the necessary variables:
- `m` and `n` to store the number of rows and columns of the matrix, respectively.
- `matrix` to store the original matrix.
- `transpose` to store the transposed matrix.
2. Accept user input for the number of rows (`m`) and columns (`n`) of the matrix.
3. Accept user input for the elements of the matrix using nested loops. The outer loop iterates over the rows, and the inner loop iterates over the columns.
4. Print the original matrix using nested loops. The outer loop iterates over the rows, and the inner loop iterates over the columns.
5. Find the transpose of the matrix using nested loops:
- The outer loop iterates over the rows of the transposed matrix (columns of the original matrix).
- The inner loop iterates over the columns of the transposed matrix (rows of the original matrix).
- Assign the element at the i-th row and j-th column of the original matrix to the j-th row and i-th column of the transposed matrix.
6. Print the transposed matrix using nested loops. The outer loop iterates over the rows, and the inner loop iterates over the columns.
Here's the C code that implements the above steps:
C
#include <stdio.h>
int main() {
int m, n;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Original Matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
int transpose[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
transpose[i][j] = matrix[j][i];
}
}
printf("Transposed Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
The program first accepts the dimensions of the matrix (`m` and `n`) from the user. Then, it accepts the elements of the matrix using nested loops.
After accepting the matrix, the program prints the original matrix.
To find the transpose, the program uses nested loops to interchange the rows and columns. The outer loop iterates over the rows of the transposed matrix (columns of the original matrix), and the inner loop iterates over the columns of the transposed matrix (rows of the original matrix). The element at the i-th row and j-th column of the original matrix is assigned to the j-th row and i-th column of the transposed matrix.
Finally, the program prints the transposed matrix.
Print the transpose of a matrix using for loop
To print the transpose of a matrix using a for loop, we need to swap the rows and columns of the original matrix. The element at the i-th row and j-th column in the original matrix becomes the element at the j-th row and i-th column in the transposed matrix.
Let's see the step-by-step explanation of printing the transpose of a matrix using a for loop:
1. Start with a given matrix, typically represented as a 2D array. Let's assume the matrix has `m` rows and `n` columns.
2. Create a new matrix or 2D array to store the transposed matrix. The transposed matrix will have `n` rows and `m` columns.
3. Use nested for loops to traverse the original matrix and populate the transposed matrix:
- The outer loop iterates over the columns of the transposed matrix (rows of the original matrix).
- The inner loop iterates over the rows of the transposed matrix (columns of the original matrix).
- Assign the element at the i-th row and j-th column of the original matrix to the j-th row and i-th column of the transposed matrix.
4. Print the transposed matrix using nested for loops:
- The outer loop iterates over the rows of the transposed matrix.
- The inner loop iterates over the columns of the transposed matrix.
- Print each element of the transposed matrix.
Let's see the implementation in C:
C
#include <stdio.h>
#define ROWS 3
#define COLS 4
int main() {
int matrix[ROWS][COLS] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int transpose[COLS][ROWS];
// Transpose the matrix
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
transpose[i][j] = matrix[j][i];
}
}
// Print the transposed matrix
printf("Transposed Matrix:\n");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, the original matrix is defined as a 2D array `matrix` with 3 rows and 4 columns. The transposed matrix is stored in a separate 2D array `transpose` with 4 rows and 3 columns.
The first set of nested for loops is used to transpose the matrix. The outer loop iterates over the columns of the transposed matrix (rows of the original matrix), and the inner loop iterates over the rows of the transposed matrix (columns of the original matrix). The element at the j-th row and i-th column of the original matrix is assigned to the i-th row and j-th column of the transposed matrix.
The second set of nested for loops is used to print the transposed matrix. The outer loop iterates over the rows of the transposed matrix, and the inner loop iterates over the columns of the transposed matrix. Each element of the transposed matrix is printed.
The output of this program will be:
Transposed Matrix :
1 5 9
2 6 10
3 7 11
4 8 12
This example demonstrates how to print the transpose of a matrix using for loops in C. The same concept can be applied in other programming languages as well.
Transpose of a Matrix using Pointers
Using pointers to find the transpose of a matrix in C enhances efficiency by providing direct memory access and reducing array indexing overhead. Instead of using array subscripts, we manipulate matrix elements using pointer arithmetic.
C Program to Find the Transpose of a Matrix using Pointers
#include <stdio.h>
#define ROW 3
#define COL 3
void transposeMatrix(int *matrix, int *transpose, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
*(transpose + j * row + i) = *(matrix + i * col + j);
}
}
}
void printMatrix(int *matrix, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%d ", *(matrix + i * col + j));
}
printf("\n");
}
}
int main() {
int matrix[ROW][COL] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int transpose[COL][ROW];
printf("Original Matrix:\n");
printMatrix((int *)matrix, ROW, COL);
transposeMatrix((int *)matrix, (int *)transpose, ROW, COL);
printf("\nTransposed Matrix:\n");
printMatrix((int *)transpose, COL, ROW);
return 0;
}
Frequently Asked Questions
What is the transpose of the matrix M × N?
The transpose of an M × N matrix is an N × M matrix obtained by swapping its rows with columns, i.e., element (i, j) becomes (j, i).
What is the formula for the transpose of a matrix?
For a matrix A with elements A[i][j], its transpose Aᵀ is obtained using the formula Aᵀ[j][i] = A[i][j], swapping rows and columns.
Can you transpose a 2×2 matrix?
Yes, transposing a 2×2 matrix swaps its off-diagonal elements, while diagonal elements remain unchanged, forming a mirrored version along the main diagonal.
How can we find the transpose of a matrix using C programming language?
A 2-dimensional array is declared and initialized, and the entries are made for the given matrix. Another 2-dimensional array is declared. The rows and columns are interchanged and are stored in the second(transpose matrix) array. Then, the transpose matrix is printed.
What is a matrix in C programming?
A matrix (plural: matrices) is a rectangular array or table of numbers arranged in rows and columns to represent a mathematical object or one of its properties. In C programming we can store a matrix using a two-dimensional array.
How to Transpose a 3x3 Matrix?
To transpose a 3x3 matrix, swap rows with columns. For element at position (i, j) in the original matrix, place it at (j, i) in the transposed matrix. Use nested loops to iterate through and assign values appropriately.
Conclusion
In this blog, we learned about the approach and the code for finding the transpose of a given matrix using C language. We hope that this blog has helped you enhance your knowledge.
Recommended Readings: