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

Discover My FlutterFlow Courses and Template Apps

Launch Your Dating App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Dating App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Grocery Delivery App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Grocery Delivery App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Courses App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Courses App with Our Step-by-Step Course and Ready-Made Template.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com