✓ Link copied to clipboard
C 📅 July 10, 2026 ⏱ 5 min read

C Program to Find Transpose of a Matrix with Example

C Program to Find Transpose of a Matrix with Example

Finding the transpose of a matrix is a common task in C programming. You've got a 2x3 matrix? Its transpose will end up being a 3x2 matrix, where rows become columns. Let’s check out how to implement this in C.

Prerequisites for Finding the Transpose in C

Here’s what you need:

  • Understanding of 2D arrays
  • Loops in C (for loop)
  • Basic I/O functions: scanf and printf
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Find Transpose of a Matrix

Here's the complete code to find the transpose of a given matrix.

transpose_matrix.cC▶ Run
#include <stdio.h>

int main() {
    int a[10][10], transpose[10][10], row, col, i, j;
    // Read rows and columns
    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &row, &col);
    // Read matrix elements
    printf("Enter elements of the matrix:\n");
    for (i = 0; i < row; ++i) {
        for (j = 0; j < col; ++j) {
            printf("Enter element a[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }
    // Transpose the matrix
    for (i = 0; i < row; ++i) {
        for (j = 0; j < col; ++j) {
            transpose[j][i] = a[i][j];
        }
    }
    // Output the transposed matrix
    printf("Transpose of the matrix:\n");
    for (i = 0; i < col; ++i) {
        for (j = 0; j < row; ++j) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Transpose of a Matrix Program

Here's what you see when you run the program. We entered a 2x3 matrix.

OUTPUTtext
Enter the number of rows and columns: 2 3
Enter elements of the matrix:
Enter element a[0][0]: 1
Enter element a[0][1]: 2
Enter element a[0][2]: 3
Enter element a[1][0]: 4
Enter element a[1][1]: 5
Enter element a[1][2]: 6
Transpose of the matrix:
1 4 
2 5 
3 6 
Output of C Program to Find Transpose of a Matrix
Output of C Program to Find Transpose of a Matrix

Algorithm to Transpose a Matrix in C

The algorithm to find the transpose of a matrix involves rearranging the rows and columns. Here's a step-by-step guide to how the program works.

  1. Read the number of rows and columns from the user.
  2. Input the matrix elements from the user.
  3. Initialize the transpose matrix.
  4. For each element, set transpose[j][i] = matrix[i][j].
  5. Output the transpose matrix.
flowchart TD
  A([Start]) --> B[/Read rows and columns/]
  B --> C[/Read matrix elements/]
  C --> D[Initialize transpose matrix]
  D --> E{More elements?}
  E -->|Yes| F["transpose[j][i] = matrix[i][j]"]
  F --> G[Increment indices]
  G --> E
  E -->|No| H[/Output transpose matrix/]
  H --> I([End])
Flowchart: the algorithm step by step

How the Transpose Program Works

Let's break down the code line by line to see how it achieves the transpose of a matrix.

  • The program starts with the #include <stdio.h> directive, which allows the use of input/output functions.
  • Declare a 2D array a and another array transpose to store the transposed matrix.
  • Use printf to prompt the user to enter the number of rows and columns, and read them using scanf.
  • Loops through each element of the matrix to input its values using nested for loops.
  • Nested loops again to compute the transpose, swapping i and j indices.
  • Finally, output the transposed matrix using printf within nested loops.
  • The program ends with return 0, indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the transpose of a matrix involve?

🎉Correct! Transpose involves swapping rows with columns.
🤔Not quite. Consider what happens to the matrix's dimensions.

Time and Space Complexity of the Transpose Program

Here's the complexity of the transpose operation.

Time Complexity: O(n * m), where n is the number of rows and m is the number of columns. This is because the program visits each element of the matrix once with the loop for (i = 0; i < row; ++i) running fully for each column. For example, with a 3x3 matrix, that means visiting 9 elements.

Space Complexity: O(n * m) since an additional matrix transpose of the same size as the original is used to store the result. Handles as many elements as a, so for a 2x3 matrix, it involves storing 6 elements.

⚠️

Common mistake: Not swapping the indices i and j when copying elements into the transpose matrix leads to wrong transpositions.

Key takeaway: The transpose operation switches rows and columns, rearranging the whole matrix.

FAQs About Finding Transpose of a Matrix in C

Here are some common questions about transposing matrices in C.

A matrix's transpose is made by swapping its rows and columns.
It's used in lots of math computations, data analysis, and algorithms.
Sure, any matrix can be transposed, square or not.
Yep, rows and columns switch places, changing the dimensions.
You do this by accessing and assigning elements from matrix[i][j] to transpose[j][i].
Diagonal elements stay in the same spot for a square matrix.

If you found this program helpful, check out these related programs:

Conclusion

Transpose is a basic operation in matrix manipulation. It's super useful in data analysis and mathematical transformations. Go ahead and experiment with different matrix sizes to see how it changes the output structure.

Practice more with our C programming quizzes or try running this and other codes in the EduSeekho online compiler.