Transpose of a Matrix in C

In this example, You will find a program for transpose of a matrix in C.

The program read elements of matrix and then finds the transpose of a matrix in C by just interchanging the rows and columns i.e. A[rows][cols] to A[cols][rows].

Transpose of a Matrix in C


#include<stdio.h>
 
int main() {
	
	int i,j,rows,cols;
	
	printf("How many Rows:");
	scanf("%d",&rows);
	printf("How many Columns:");
	scanf("%d",&cols);
	
	int A[rows][cols];
	
	printf("\nEnter the matrix:\n");
	for(i=0;i<rows;i++)
		for(j=0;j<cols;j++)
			scanf("%d",&A[i][j]);
			
	printf("\nTranspose of given matrix:\n");
	
	for(i=0;i<rows;i++)
	{
		for(j=0;j<cols;++j)
		printf("%d ",A[j][i]);
		printf("\n");
	}
	return 0;
}

Output


 How many Rows:3
 How many Columns:3

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

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

No comments:

If you have any doubts, Please let me know

Powered by Blogger.