Example HTML page

C Program for Transpose of a given Matrix

In this section, we are writing a program for Transposing a given matrix with order mXn.

For example, if the order of matrix is 2X2.

Then, to do the transpose, we require one input matrix and another matrix for storing the result.

For example:

First matrix:

1 2

3 4

result matrix:

1 3

2 4

To write this program, do the following things.

Step 1: read the order of the matrix

Step 2: read the first matrix

Step 3: write the logic for transposing a matrix and store it in result matrix.

Step 5: print the result matrix.

#include <stdio.h>
  
int main()
{
   int  first[10][10], result[10][10],m, n, i,j;
   // read the order of the matrix.
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
    
   // reading the first matrix 
   printf("Enter the elements of first matrix\n");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first[i][j]);
          
   
 // Transposing logic
      
   for (i = 0; i < m; i++)
   {
    for (j = 0; j < n; j++)
        {
            result[i][j]=first[j][i];
        }
   }
        
 // printing the elements of resultant matrix
 printf("The transpose of a given matrix is:\n");
   for (i=0;i<m;i++)
   {
    for (j=0;j<n;j++)
    {
        printf("%d ",result[i][j]);
       }
       printf("\n");
   }
  
   return 0;
}

Transpose of a Matrix

Output:

Command Prompt

Enter the number of rows and columns of matrix
3
3
Enter the elements of first matrix
1 2 3
4 5 6
7 8 9
The transpose of a given matrix is:
1 4 7
2 5 8
3 6 9

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest