C Code to transpose a matrix - Coding Bot

Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Tuesday, 27 December 2016

C Code to transpose a matrix

Transposing a matrix means interchanging the rows and column elements of a matrix. For example, if we have a matrix:

1 2 3
4 5 6
7 8 9

Then its transpose will be:

1 4 7
2 5 8
3 6 9



Here is the C Code to find transpose of a matrix:


/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>

void main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
 
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
 
   printf("Enter the elements of matrix\n");
 
   for (c = 0; c < m; c++)
      for(d = 0; d < n; d++)
         scanf("%d",&matrix[c][d]);
 
   for (c = 0; c < m; c++)
      for( d = 0 ; d < n ; d++ )
         transpose[d][c] = matrix[c][d];
 
   printf("Transpose of entered matrix :-\n");
 
   for (c = 0; c < n; c++) {
      for (d = 0; d < m; d++)
         printf("%d\t",transpose[c][d]);
      printf("\n");
   }
}


Output:


C Code to transpose a matrix



Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: