C Program to perform Matrix Transpose

Perform Matrix Transpose

In the following example, we will transpose the given matrix (two-dimensional array).

Example

C Compiler
#include <stdio.h> int main() { int i, j, n; int arr[3][3] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }; printf("Matrix (3 X 3):\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d ", arr[i][j]); printf("\n"); } for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = n; } } printf("\nMatrix Transpose:\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d ", arr[i][j]); printf("\n"); } return 0; }

In the following example, we will get the values for (3 x 3) Matrix from the user and display the transpose matrix.

Example

C Compiler
#include <stdio.h> int main() { int i, j, n; int arr[3][3]; printf("Enter Matrix (3 X 3):\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &arr[i][j]); for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = n; } } printf("\nMatrix Transpose:\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d ", arr[i][j]); printf("\n"); } return 0; }

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author