Transpose of a matrix in c

Transpose of a matrix in c programming is the way to obtain a new matrix by transposing it with the help of c programming language.

What is transpose of a matrix?

Transpose of a matrix can be done by creating a new matrix by exchanging the rows and columns.

If we want to obtain transpose we can write its rows as columns and colums as rows.


Program to find transpose of a matrix in c

/*c program to find the transpose of a matrix
  Matrix transpose in c
*/
#include<stdio.h>
int main(int argc, char const *argv[])
{
	int a,b;
	printf("\n\n\tEnter number and rows of matrix:\n\t");
	scanf("%d%d",&a,&b);
	int matrix[a][b],result[b][a];
	printf("\n\tEnter matrix\n\t");
	for( int i=0 ; i<a; i++ )
	{
		for( int j=0 ; j<b ; j++ )
		{
			scanf("%d",&matrix[i][j]);
		}
	}

	printf("\n\tTransposing the matrix\n\t");
	for( int i=0 ; i<a; i++ )
	{
		for( int j=0 ; j<b ; j++ )
		{
			result[i][j]=matrix[j][i];
		}
	}
	printf("\n\tResultant matrix is:\n\t");
	for( int i=0 ; i<a; i++ )
	{
		printf("\n\t");
		for( int j=0 ; j<b ; j++ )
		{
			printf("%d ",result[i][j]);
		}
	}
	printf("\n\n\n");
	return 0;
}

Code explanation

First we have to take input matrix array.

In a nested for loop we are interchanging the rows as columns and columns as rows.

Then we are printing the result.

Output

	//program to find transpose of a matrix in c
       Enter number and rows of matrix:
	2 2

	Enter matrix
	1 2 3 4

	Transposing the matrix
	
	Resultant matrix is:
	
	1 3 
	2 4 
Output for transpose of a matrix in c 1
	//program to find transpose of a matrix in c
        Enter number and rows of matrix:
	3 3

	Enter matrix
	1 2 3 4 5 6 7 8 9

	Transposing the matrix
	
	Resultant matrix is:
	
	1 4 7 
	2 5 8 
	3 6 9 
Output for transpose of a matrix in c 2

People also ask

What is transpose of a matrix?

Transpose matrix can be obtained by interchanging rows and columns.

How to find transpose of a matrix in c

First take an input matrix array. In a nested for loop interchange the rows as columns and columns as rows. Then we get the resultant matrix.


People also read