Write a C Program to transpose a matrix

Article by: Manish Methani

Last Updated: September 26, 2021 at 10:04am IST
1 min 21 sec

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

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>