In this tutorial, you will learn a writing program of Transpose matrix in C with example.

To learn writing a program for finding transpose of a matrix, you should have some knowledge of

  • Array and
  • Multidimensional Array

Also, Writing the program of Transpose matrix in C with example is now that much tough.

We can obtain transpose of a matrix by exchanging the rows and columns. Matrix made with the help of rows and columns or we can say that every matrix has row and column. Now if you want to generate the transpose of the matrix you don’t have to do anything, just exchange rows element into column and column elements into a row.

Below is the c program to find the transpose of a matrix where the user is asked to enter the number of rows r and columns c. Their values should be less than 5 in this program.

Then, the user is asked to enter the elements of the matrix (of order r*c).

Program to Find transpose matrix in C

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
#include <stdio.h>
int main() {
int a[5][5], transpose[5][5], r, c, i, j;
printf("Please enter number of rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }

    // Showing the matrix a[][]
    printf("\nEntered matrix: \n");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("%d  ", a[i][j]);
            if (j == c - 1)
                printf("\n");
        }

    // Finding the transpose of matrix a
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            transpose[j][i] = a[i][j];
        }

    // showing the transpose of matrix a
    printf("\nTranspose of the matrix:\n");
    for (i = 0; i < c; ++i)
        for (j = 0; j < r; ++j) {
            printf("%d  ", transpose[i][j]);
            if (j == r - 1)
                printf("\n");
        }
    return 0;
}

Output

C Program to Find Transpose of a Matrix

Program Explanation of transpose matrix in C

In the above program we have taken two matrix name is “x” and “transpose” having five rows and columns in each.

Now with the help of for loop I have tried to insert values in each rows and columns. Now its time to shift all rows elements in columns and each columns into rows.

transpose[j][i] = a[i][j];

Above code is doing the rows into column movement.

Thanks for reading 🙂