Write a program in C to find transpose of a given matrix

Previous
Next

Solution:

  • The number of rows and columns in A is equal to number of columns and rows in B respectively.
  • Thus, the matrix B is known as the Transpose of the matrix A. The transpose of matrix A is represented by A′ or AT.
  • The following statement generalizes transpose of a matrix:
    • If A = [aij]m×n, then A′ =[aij]n×m.
#include<stdio.h>
int main()
{
	int i, j, mat[3][3],temp;

	printf("Enter 3x3 matrix elements : \n");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			scanf("%d", &mat[i][j]);
		}
	}
	printf("Input Matrix : \n");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			printf("%d ", mat[i][j]);
		}
		printf("\n");
	}
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			if(i<j)
			{
				temp = mat[i][j];
				mat[i][j] = mat[j][i];
				mat[j][i] = temp; 
			}
		}
	}
	printf("Transpose matrix : \n");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			printf("%d ", mat[i][j]);
		}
		printf("\n");
	}
	return 0;
}
Previous
Next

Courses Enquiry Form