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.

**Example**

**Input**

Input elements in matrix: 1 2 3 4 5 6 7 8 9

**Output**

Transpose: 1 4 7 2 5 8 3 6 9

## Required knowledge

Basic C programming, For loop, Array

### 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 **A**^{T}.

Transpose of a matrix **A** is defined as –

**A**^{T}_{ij} = **A**_{ji} ; 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: 1 2 3 4 5 6 7 8 9 Original matrix: 1 2 3 4 5 6 7 8 9 Transpose of matrix A: 1 4 7 2 5 8 3 6 9

Happy coding 😉

### Recommended posts

- Array and Matrix programming exercises index.
- C program to find determinant of a matrix.
- C program to check Identity matrix.
- C program to check Symmetric matrix.
- C program to check Sparse matrix.
- C program to find sum of main diagonal elements of a matrix.
- C program to find sum of opposite diagonal elements of a matrix.