Transpose of Matrix in C

Here is the program for transpose of matrix in C.

We first read a matrix of size mxn and then find its transpose by just interchanging the rows and columns i.e. rows become columns and columns become rows.

Transpose of Matrix in C

#include<stdio.h>

int main()
{
	int a[5][5],i,j,m,n;
	
	printf("How many rows?");
	scanf("%d",&n);
	printf("How many columns?");
	scanf("%d",&m);
	
	printf("\nEnter the matrix:\n");
	for(i=0;i<m;++i)
		for(j=0;j<n;++j)
			scanf("%d",&a[i][j]);
			
	printf("\nTranspose of given matrix:\n");
	
	for(i=0;i<m;++i)
	{
		for(j=0;j<n;++j)
			printf("%d ",a[j][i]);
		
		printf("\n");
	}

	return 0;
}

 

Output

How many rows?3
How many columns?3

Enter the matrix:
1 2 3
4 5 6
7 8 9

Transpose of given matrix:
1 4 7
2 5 8
3 6 9

Leave a Comment

Your email address will not be published. Required fields are marked *