# Java program to find transpose of a matrix

Sure, here's a Java program to find the transpose of a matrix:

```public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

System.out.println("Original matrix:");
printMatrix(matrix);

int[][] transpose = getTranspose(matrix);

System.out.println("Transpose matrix:");
printMatrix(transpose);
}

public static int[][] getTranspose(int[][] matrix) {
int rows = matrix.length;
int cols = matrix.length;

int[][] transpose = new int[cols][rows];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

return transpose;
}

public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}```

In this program, we first define a two-dimensional integer array `matrix` to represent the original matrix. We then print out the original matrix using the `printMatrix` method.

We then call the `getTranspose` method to obtain the transpose of the matrix. This method takes the original matrix as input and returns the transpose as a new two-dimensional integer array.

To obtain the transpose, we first obtain the number of rows and columns in the original matrix. We then create a new two-dimensional integer array `transpose` with the dimensions switched (i.e., the number of columns in the original matrix becomes the number of rows in the transpose, and vice versa).

We then use nested `for` loops to iterate through each element of the original matrix and assign it to the corresponding position in the transpose. Note that we switch the row and column indices when assigning the elements to the transpose.

Finally, we print out the transpose matrix using the `printMatrix` method.

The `printMatrix` method is a helper method that takes a two-dimensional integer array as input and prints out each element of the array in row-major order.