Home >>Java Programs >Java Program to find the transpose of a given matrix
In this example, we will create a java program to find the transpose of the given matrix and print the resulting matrix.
Transpose of a matrix can be found by interchanging the rows of the matrix with the column and the columns with the row.
public class Main
{
public static void main(String[] args)
{
int rows, cols;
int a[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
rows = a.length;
cols = a[0].length;
int t[][] = new int[cols][rows];
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
t[i][j] = a[j][i];
}
}
System.out.println("Transpose of given matrix: ");
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
System.out.print(t[i][j] + " ");
}
System.out.println();
}
}
}