**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;
}
```

Check out this problem - __Matrix Median__

### 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;

}

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;

}

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.

**Frequently Asked Questions**

**What is the transpose of a matrix in C?**

Transpose of a matrix is obtained by interchanging all the rows and columns of the base matrix, i.e., all the rows of the base matrix will be columns of the transpose matrix and vice versa.

**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.

**Conclusion**

In this blog, we learnt 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, and if you wish to learn more, check out our __Cod____e360__ Blog site and visit our__ Library__.

Also read __reverse a number____.__