Advertisement

 /* Write a program in C to find transpose of a given matrix. */
#include <stdio.h>
int main()
{
  int a[50][50],i,j,r,c;
  printf("Enter the rows and columns of the matrix : ");
  scanf("%d %d",&r,&c);
  for(i=0;i<r;i++)
    for(j=0;j<c;j++)
    {
        printf("element - [%d][%d] : ",i,j);
        scanf("%d",&a[i][j]);
    }
  printf("\nThe matrix is :\n");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
        printf("%d ",a[i][j]);
    printf("\n");
  }
  printf("Transpose of a given matrix :\n");
  for(i=0;i<c;i++)
  {
      for(j=0;j<r;j++)
        printf("%d ",a[j][i]);
      printf("\n");
  }
  return 0;
}

Output:
Enter the rows and columns of the matrix : 2 3
element - [0][0] : 1
element - [0][1] : 2
element - [0][2] : 3
element - [1][0] : 4
element - [1][1] : 5
element - [1][2] : 6

The matrix is :
1 2 3
4 5 6
Transpose of a given matrix :
1 4
2 5
3 6

Back

Post a Comment

0 Comments