C Program to Transpose a square Matrix


 

// Program by Akash Tripathi (@proakash256)  
#include <stdio.h>
int main()
{
    int r;
    printf("Enter the number of Rows and Columns: ");
    scanf("%d" , &r);
    int ar1[r][r];
    int ar2[r][r];
    printf("\nEnter the elements (row wise) of the Matrix:\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
        {
            scanf("%d" , &ar1[i][j]);
        }
    }
    printf("\nThe Matrix is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
        {
            ar2[i][j] = ar1[j][i];
            printf("%d\t" , ar1[i][j]);
        }
        printf("\n");
    }
    printf("\nThe Transposed Matrix is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
            printf("%d\t" , ar2[i][j]);
        printf("\n");
    }
    return 0;
}

Comments