Matrix manipulation is a fundamental abstract for computer programming, particularly in scientific and mathematical applications. One crucial operation is the transpose of a matrix in which we have to swap its rows and columns. In this article, we are going to write a C program to find a transpose of a matrix.
Table of Contents
Matrix Transpose
The transpose of a matrix is a new matrix formed by interchanging it’s rows with columns.
C Program to Find Transpose of a Matrix
#include <stdio.h>
void main()
{
int row, column;
printf("Enter the number of rows ");
scanf("%d", &row);
printf("Enter the number of column ");
scanf("%d", &column);
int matrix[row][column], transpose[column][row];
printf("Enter the element of Matrix\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
printf("Enter the element at position {%d,%d}", i, j);
scanf("%d", &matrix[i][j]);
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
transpose[j][i] = matrix[i][j];
}
}
printf("\noriginal matrix \n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\nTranspose of matrix \n");
for (int i = 0; i < column; i++)
{
for (int j = 0; j < row; j++)
{
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
}
Output
How the Above C Program Works?
- The program starts with the main method and the user enters the matrix. If you don’t know how to take matrix input in C refer to this tutorial – Matrix Input and Output in C
- After that, we calculate a transpose of the matrix by using nested loop and interchange values of rows with the columns.
- At the end, we print both matrices original matrix entered by the user, and transpose of matrix found by our C program.
Happy Coding
1 thought on “Transpose of Matrix in C”