Program for matrix transpose in c is an implementation that performs transposition of the given matrix.

What is matrix: It is a collection of numbers arranged in rows and columns.

What is Transpose matrix?
Transpose of a matrix: The transpose of a matrix is a new matrix in which rows are columns in the original matrix.

How to transpose a matrix?
Take an input matrix with specific rows and columns. To transpose matrix exchange rows and column values then after swapped we rows and columns values read a value of the matrix row-wise and copy to a temporary matrix. Print transpose of the matrix.

Transpose matrix example

Transpose matrix

Algorithm for Matrix transpose in c

  1. Declare variable
  2. Take the input matrix with rows and column
  3. Exchange rows and column value
  4. Read and copy matrix to temporary matrix
  5. Print transpose matrix

Program for matrix transpose in c

#include<stdio.h>
main()
{
int i,j,rows,col,temp;
printf("Enter number of rows and columns of a matrix\n");
scanf("%d %d",&rows,&col);


int a[rows][col],t[rows][col];
//Taking input of matrix
printf("Enter Matrix 1\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given /matrix is\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",a[i][j]);
}

printf("\n");
}
//Exchanging rows and columns
temp=rows;
rows=col;
col=temp;
//Transpose of matrix
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
t[i][j]=a[j][i];
}
}

printf("Transpose of above matrix is\n");

for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",t[i][j]);
}

printf("\n");
}

}

Output :

Matrix transpose in c

Write A Comment