C Online Compiler
Example: Transpose of Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Transpose of Matrix #include <stdio.h> int main() { int rows, cols; // Step 1: Get matrix dimensions from the user printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &cols); int originalMatrix[rows][cols]; int transposedMatrix[cols][rows]; // Transposed matrix will have dimensions cols x rows // Step 2: Get elements of the original matrix printf("\nEnter elements of the original matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Enter element originalMatrix[%d][%d]: ", i + 1, j + 1); scanf("%d", &originalMatrix[i][j]); } } // Step 3: Print the original matrix printf("\nOriginal Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d\t", originalMatrix[i][j]); } printf("\n"); } // Step 4: Perform transposition for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposedMatrix[j][i] = originalMatrix[i][j]; } } // Step 5: Print the transposed matrix printf("\nTransposed Matrix:\n"); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { printf("%d\t", transposedMatrix[i][j]); } printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS