Saturday, December 4, 2021
HomeTop Coding QuestionWrite a program to find transpose of matrix.

# Write a program to find transpose of matrix.

Given two matrix we have to calculate transpose of matrix.

so what is transpose matrix?

transpose matrix is a matrix which we get when we exchange values of rows with column.

Example:

``````input:
1 2
3 4
output:
1 3
2 4``````

Logic:

``exchange the row and column values with each other.``

```#include<bits/stdc++.h>
using namespace std;
int main() {
int matrix[10][10], transpose[10][10];
int row, col, i, j;
cout<<"Enter rows and columns: "<<endl;
cin>>row>>col;

cout<<" Enter matrix elements:"<<endl;
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
cin>>matrix[i][j];
}
}

for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j) {
transpose[j][i] = matrix[i][j];
}
}

cout<<"Transpose of the matrix:"<<endl;
for (i = 0; i < col; ++i)
{
for (j = 0; j < row; ++j) {
cout<<transpose[i][j]<<" ";
if (j == row-1)
{
cout<<endl;
}
}   }

}
```

```Enter rows and columns:  2 2
Enter matrix elements:
1 2
3 4
Transpose of the matrix:
1 3
2 4
```

Time Complexity-O(N*N)

Space Complexity-O(N*N)