This is a Cpp Program which prints the transpose of a given matrix. Transpose of a matrix is obtained by interchanging the rows and columns of a given matrix. |
See the figure below :
![]() |
| Transpose of a given matrix |
#include <iostream>
using namespace std;
int main()
{
int rows,columns,i,j;
cout << "Enter order of matrix :\t";
cin >> rows;
cin >> columns;
int matrix[rows][columns];
cout << "\nEnter elements of a matrix\n";
for(i=0; i<rows; i++)
{
for(j=0; j<columns; j++)
{
cout << "Enter element m" << i+1 << j+1 << ":\t";
cin >> matrix[i][j];
}
}
cout << "\nMATRIX is :\n";
for(i=0; i<rows; i++)
{
for(j=0; j<columns; j++)
{
cout << "\t" << matrix[i][j];
}
cout << "\n";
}
cout << "\nIts TRANSPOSE is:\n";
for(j=0; j<columns; j++)
{
for(i=0; i<rows; i++)
{
cout <<"\t" << matrix[i][j];
}
cout << "\n";
}
return 0;
}
OUTPUT :
| Cpp Program to find transpose of a matrix |
