# Java program to print the transpose of a matrix

## Transpose of a Matrix

In this article, we will discuss the java program to print the transpose of a matrix. Converting rows of matrix into columns and columns of a matrix into rows is called transpose of a matrix. In other words, transpose of matrix[][] is obtained by changing matrix[i][j] to matrix[j][i]. ## Transpose of a Matrix in Java

A transpose of a matrix is the matrix flipped over its diagonal i.e. the row and column indices of the matrix are switched. An example of this is given as follows −

```Matrix =
1 2 3
4 5 6
7 8 9
Transpose of Matrix  =
1 4 7
2 5 8
3 6 9```

## Code in java

`import java.util.*;class Solution{    public static void main(String[] args)    {         Scanner sc=new Scanner(System.in);         System.out.println("Enter the number of rows of matrix");         int row=sc.nextInt();         System.out.println("Enter the number of columns of a matrix");         int col=sc.nextInt();         int matrix[][]=new int[row][col];         System.out.println("Enter the elements of a matrix");           for(int i=0;i<row;i++)           for(int j=0;j<col;j++)             matrix[i][j]=sc.nextInt();           int result[][]=new int[col][row];         for(int i=0;i<col;i++)             for(int j=0;j<row;j++)               result[i][j]=matrix[j][i];                   System.out.println("Transpose of a matrix ");          for(int i=0;i<col;i++)          {                for(int j=0;j<row;j++)                   System.out.print(result[i][j]+" ");                 System.out.println();           }    }}`

## Output :

`Enter the number of rows of matrix3Enter the number of columns of a matrix3Enter the elements of a matrix1 2 34 5 67 8 9Transpose of a matrix1 4 72 5 83 6 9`