Saturday, 28 September 2024

Write a Program to Transpose Matrix

 #include <stdio.h>


int main() {

    // Initialize a 2x2 matrix

    int matrix[2][2];


    // Input the matrix elements

    printf("Enter elements of 2x2 matrix:\n");

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < 2; j++) {

            printf("Element [%d][%d]: ", i, j);

            scanf("%d", &matrix[i][j]);

        }

    }


    // Display the original matrix

    printf("\nOriginal Matrix:\n");

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < 2; j++) {

            printf("%d ", matrix[i][j]);

        }

        printf("\n");

    }


    // Calculate and display the transpose of the matrix

    printf("\nTranspose of the Matrix:\n");

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < 2; j++) {

            printf("%d ", matrix[j][i]);

        }

        printf("\n");

    }


    return 0;

}