
transpose a matrix?

Java find transpose of matrix
The given code determine the transpose of a matrix.The transpose of a matrix is formed by interchanging the rows and columns of a matrix such that row i of matrix becomes column i of the transposed matrix. The transpose of A is denoted by AT. Through the following code,the user is allowed to enter the number of rows and columns and the elements to create a matrix. Then using the for loop statements, turn all the rows of the matrix into columns and vice-versa.
import java.util.*;
public class Transpose {
public static void main(String[] args) throws Exception {
int rows, cols;
int[][] matrix, tmatrix;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = input.nextInt();
System.out.print("Enter number of columns: ");
cols = input.nextInt();
matrix = new int[rows][cols];
tmatrix = new int[cols][rows];
System.out.println("Enter elements for Matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = input.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
tmatrix[i][j] = matrix[j][i];
}
}
System.out.println("Matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Its transpose is: ");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(tmatrix[i][j] + " ");
}
System.out.println();
}
}
}