/  C Programs   /  C program to seek out the transpose of a given matrix

C program to seek out the transpose of a given matrix

The transpose of a given matrix is formed by exchanging the rows and columns of a matrix.

 

#include <stdio.h>
int main() {
   int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("\nEnter elements in a matrix:\n");
   for (i = 0; i<r; i++)
      for (j = 0; j<c; j++) {
scanf("%d", &a[i][j]);
    }

printf("\nEntered matrix: \n");
   for (i = 0; i<r; i++)
      for (j = 0; j<c; j++) {
printf("%d  ", a[i][j]);
         if (j == c - 1)
printf("\n");
      }

   for (i = 0; i<r; i++)
      for (j = 0; j<c; j++) {
         transpose[j][i] = a[i][j];
      }

printf("\nTranspose of the matrix:\n");
   for (i = 0; i<c; i++)
      for (j = 0; j<r; j++) {
printf("%d  ", transpose[i][j]);
        if (j == r - 1)
printf("\n");
      }
   return 0;
}

 

Output:

 

C program to seek out the transpose of a given matrix

 

Leave a comment