C program For Transpose Matrix

In this C program, We will show “How to build logic for Transpose Matrix ” with easily. We, also see, “How to print a Transpose Matrix taking and without taking user input”. So, let’s do it …

Printing Transpose Matrix without taking User Input

#include<stdio.h>.
int main()
{
  int i, j, A[3][2] = {{1,2},{3,4},{5,6}},T[2][3];

  //Printing The A Matrix.
  printf("A Matrix is: \n");
  for(i=0; i<3; i++)
    {
    for(j=0; j<2; j++)
      {
         printf("%d ",A[i][j]);
      }
      printf("\n");
    }

  //Applying Condition for Transpose Matrix.
  for(i=0; i<3; i++)
  {
    for(j=0; j<2; j++)
      {
         T[j][i] = A[i][j];
      }
      printf("\n");
  }


  //printing The Transpose Matrix.
  printf("Transpose Matrix: \n");
  for(i=0; i<2; i++)
  {
      for(j=0; j<3; j++)
      {
         printf("%d ",T[i][j]);
      }
      printf("\n");
  }

  return 0;
}

Printing Transpose Matrix Taking User Input

#include<stdio.h>
int main()
{
    int A[30][30], T[30][30], i, j, rows, colm;

    printf("Enter the rows and columns for the matrix: ");
    scanf("%d %d",&rows,&colm);

    //Enter the elements for A matrix.
    printf("Enter the elements of A Matrix: \n");
    for(i=0; i<rows; i++)
    {
        for(j=0; j<colm; j++)
        {
            printf("Enter Elements A[%d][%d] : ",i,j);
            scanf("%d",&A[i][j]);
        }
    }

    printf("Matrix A: \n");
    for(i=0; i<rows; i++)
    {
        for(j=0; j<colm; j++)
        {
            printf("%d ",A[i][j]);
        }
        printf("\n");

    }

    //Applying Condition for Transpose Matrix.
    for(i=0; i<rows; i++)
    {
        for(j=0; j<colm; j++)
        {
           T[j][i] = A[i][j];
        }
    }

    //Printing The Transpose Matrix.
    printf("Transpose Matrix T: \n");
    for(i=0; i<colm; i++)
    {
        for(j=0; j<rows; j++)
        {
            printf("%d ",T[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Keep learning with PrologiCode.

Happy Coding.

Leave a Comment