# C++ Program to Find the Transpose of a Matrix

«
»

This is a C++ Program to Find the Transpose of a Matrix.

Problem Description

The program takes a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.

Problem Solution

1. Initially, the number of rows and columns of the matrix are entered, followed by the matrix elements.
2. The tranpose of the matrix is printed.
3. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find the Transpose of a Matrix. The program output is shown below.

1. `#include<iostream>`
2. `using namespace std;`
3. `int main ()`
4. `{`
5. `    int A[10][10], m, n, i, j;`
6. `    cout << "Enter rows and columns of matrix : ";`
7. `    cin >> m >> n;`
8. `    cout << "Enter elements of matrix : ";`
9. `    for (i = 0; i < m; i++)`
10. `        for (j = 0; j < n; j++)`
11. `            cin >> A[i][j];`
12. `    cout << "Entered Matrix : \n ";`
13. `    for (i = 0; i < m; i++)`
14. `    { `
15. `        for (j = 0; j < n; j++)`
16. `            cout << A[i][j] << " ";`
17. `        cout << "\n ";`
18. `    }`
19. `    cout << "Transpose of Matrix : \n ";`
20. `    for (i = 0; i < n; i++)`
21. `    {`
22. `        for (j = 0; j < m; j++)`
23. `            cout << A[j][i] << " ";`
24. `        cout << "\n ";`
25. `    }`
26. `    return 0;`
27. `}`
Program Explanation

1. The user is asked to enter the number of rows and columns of the matrix.
2. The elements of the matrix are asked to enter and stored in the matrix ‘A’.
3. The transpose is found by exchanging the rows with columns and columns with rows.
4. The original matrix and the transpose are both printed.

Runtime Test Cases
```Case 1 :
Enter rows and columns of matrix : 3 3
Enter elements of matrix : 9 8 7 6 5 4 3 2 1
Entered Matrix :
9 8 7
6 5 4
3 2 1
Transpose of Matrix :
9 6 3
8 5 2
7 4 1

Case 2 :
Enter rows and columns of matrix : 2 2
Enter elements of matrix : 1 2 3 4
Entered Matrix :
1 2
3 4
Transpose of Matrix :
1 3
2 4

Case 3 :
Enter rows and columns of matrix : 2 3
Enter elements of matrix : 0 2 4 6 8 10
Entered Matrix :
0 2 4
6 8 10
Transpose of Matrix :
0 6
2 8
4 10```

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.