# C++ Program to perform Matrix Transpose

You are Here:

## Perform Matrix Transpose

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

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, n; int arr[3][3] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }; cout << "Matrix (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\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; } } cout << "\nMatrix Transpose: \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\n"; } return 0; }

### Output

Matrix (3 x 3): 1 1 1 2 2 2 3 3 3 Matrix Transpose: 1 2 3 1 2 3 1 2 3

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 <iostream> using namespace std; int main() { int i, j, n, arr[3][3]; cout << "Enter Matrix (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cin >> 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; } } cout << "\nMatrix Transpose: \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\n"; } return 0; }

### Output

Enter Matrix (3 x 3): 1 2 3 6 7 9 4 5 7 Matrix Transpose: 1 6 4 2 7 5 3 9 7

## Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C++ with examples for quick and easy learning.

We are working to cover every Single Concept in C++.