Write a C Program to transpose a matrix

1 min 21 sec Basic


The transpose of a matrix is obtained by changing its rows into columns and its columns into rows. A rectangular array of numbers or functions that are arranged in the form of rows and columns is called a matrix.

#include <stdio.h>
#define NUMBEROFROWS      10
#define NUMBEROFCOLS      10
 
int main()
{
    int matrix[NUMBEROFROWS][NUMBEROFCOLS];
    int i,j,row,col;
    printf("Enter number of Rows :");
    scanf("%d",&row);
    printf("Enter number of Cols :");
    scanf("%d",&col);
 
    printf("Enter matrix elements :");
    for(i=0;i< row;i++)
    {
        for(j=0;j< col;j++)
        {
            printf("Enter element [%d,%d] : ",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
 
    printf("Transpose Matrix is :");
    for(i=0;i< col;i++)
    {
        for(j=0;j < row;j++)
        {
            printf("%d	",matrix[j][i]);    /*print elements*/
        }
        printf("
");   /*after each row print new line*/      
    }
    return 0;       
}

Output

    Enter number of Rows :2 
    Enter number of Cols :3 

    Enter matrix elements : 
    Enter element [1,1] : 10 
    Enter element [1,2] : 11 
    Enter element [1,3] : 12 
    Enter element [2,1] : 13 
    Enter element [2,2] : 14 
    Enter element [2,3] : 15 

    Transpose Matrix is :
    10	13	 
    11	14	 
    12	15

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python