C Program to Find Transpose of a Matrix

This program takes a matrix of order r*c from the user and computes the transpose of that matrix.

To understand this example, you should have the knowledge of following C programming topics:

In this program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 10 in this program.

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

Then, the program computes the transpose of the matrix and displays it on the screen.

Example: Program to Find Transpose of a Matrix

#include <stdio.h>

int main()
{
int a, transpose, r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

// Storing elements of the matrix
printf("\nEnter elements of matrix:\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]);
}

// Displaying 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\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];
}

// Displaying the transpose of matrix a
printf("\nTranspose of 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\n");
}

return 0;
}

Output

Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix:
2  3  4

5  6  4

Transpose of Matrix:
2  5

3  6

4  4