# C Program to Find Transpose of a Matrix

« Previous Program Next Program »

In this article, you will learn and get code on finding the transpose of given matrix by user at run-time using a C program. But before starting the program, let's first understand, how to find the transpose of any matrix.

### How to Find Transpose of a Matrix ?

Transpose of a matrix can be calculated by switching the rows with columns or columns with rows. For example, if a given matrix say Matrix A is:

```1  1  1
2  2  2
3  3  3```

Then, its transpose say AT will be:

```1  2  3
1  2  3
1  2  3```

As you can see that all the three rows gets switched to three columns in a way that:

• Element at 0th row and 0th column of original matrix, goes to 0th row and 0th column of AT
• Element at 0th row and 1st column of original matrix, goes to 1st row and 0th column of AT
• Element at 0th row and 2nd column of original matrix, goes to 2nd row and 0th column of AT
• Element at 1st row and 0th column of original matrix, goes to 0th row and 1st column of AT
• Element at 1st row and 1st column of original matrix, goes to 1st row and 1st column of AT
• Element at 1st row and 2nd column of original matrix, goes to 2nd row and 1st column of AT
• In similar way, the next three elements of original matrix goes to matrix AT

## Find Transpose of a Matrix in C

This program is created for 3*3 matrix. That is a matrix with 3 rows and 3 columns. Therefore, this program asks from user to enter 9 elements for the matrix, and then will find its transpose:

```#include<stdio.h>
#include<conio.h>
int main()
{
int mat, i, j, matTrans;
printf("Enter 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat[i][j]);
}
// Transposing the Matrix...
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matTrans[j][i] = mat[i][j];
}
printf("\nTranspose of given Matrix is:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d  ", matTrans[i][j]);
printf("\n");
}
getch();
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now enter any 9 elements for the matrix and press `ENTER` key to see the following output:

The following block of code is responsible for transposing the matrix:

```for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matTrans[j][i] = mat[i][j];
}```

If you want to learn about for loop in detail, then refer to the separate tutorial on it. But for now, the above code block executes in a way that, the initialization part say i=0 executes only once at start of the loop, and every time while entering the block of code inside the loop, its condition (second statement of loop) must be checked. If the condition evaluates to be true, then only the program flow goes inside the body of loop and executes all the statements

Let's suppose, user enters 9 elements as 1, 1, 1, 2, 2, 2, 3, 3, 3. Therefore mat=1, mat=1, mat=1, mat=2, mat=2 and so on. Because, i starts from 0 and executes until its value becomes 3, therefore loop executes total of 3 times. That is, both the loop executes 3 times in a way that:

• At first execution of outer for loop, inner for loop executes three times. Therefore
• At first execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 1
• At second execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 1
• At third execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 1
• At second execution of outer for loop, inner for loop again executes three times. Therefore
• At first execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 2
• At second execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 2
• At third execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 2
• At third execution of outer for loop, inner for loop again executes three times. Therefore
• At first execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 3
• At second execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 3
• At third execution, `matTrans[j][i] = mat[i][j]` or `matTrans = mat` or matTrans = 3

In this way, we will have a matrix named matTrans[][] that has its value as:

• matTrans = 1
• matTrans = 2
• matTrans = 3
• matTrans = 1
• matTrans = 2
• matTrans = 2
• matTrans = 1
• matTrans = 2
• matTrans = 3

which is the transpose of given matrix A.

### Allow User to Define Matrix Dimension

This program allows the user to define the dimension of original matrix. That is, user has to enter the size of row and column for the matrix. And also enter the matrix element of given size. Then will calculate and print the transpose:

```#include<stdio.h>
#include<conio.h>
int main()
{
int mat, rowSize, colSize, i, j, matTrans;
printf("Enter the Size of Row and Column: ");
scanf("%d%d", &rowSize, &colSize);
printf("\nEnter %d*%d Matrix Elements: ", rowSize, colSize);
for(i=0; i<rowSize; i++)
{
for(j=0; j<colSize; j++)
scanf("%d", &mat[i][j]);
}
printf("\nOriginal Matrix is:\n");
for(i=0; i<rowSize; i++)
{
for(j=0; j<colSize; j++)
printf("%d  ", mat[i][j]);
printf("\n");
}
// Transposing the Matrix.....
for(i=0; i<rowSize; i++)
{
for(j=0; j<colSize; j++)
matTrans[j][i] = mat[i][j];
}
printf("\nTranspose of Matrix is:\n");
for(i=0; i<colSize; i++)
{
for(j=0; j<rowSize; j++)
printf("%d  ", matTrans[i][j]);
printf("\n");
}
getch();
return 0;
}```

Here is it sample run:

#### Same Program in Other Languages

C Online Test

« Previous Program Next Program »