## Transpose of Matrix in java

Write a program to find the transpose of a square matrix of size N*N. Transpose of a matrix is obtained by changing rows to columns and columns to rows.

Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(1)

#### Input

• The first line contains an integer N.
• The next N lines contains N spaced integers each, elements of matrix.

#### Constraints

• 1 <= N <= 100
• 10^3 <= mat[i][j] <= 10^3

#### Output

Print the transposed matrix.

#### Example

Sample Input

```4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
```

Sample Output

```1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4```

## Solution of Transpose of Matrix in java :–

```import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[][] = new int[n][n];

for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
arr[i][j]=sc.nextInt();
}
}

for(int i=0;i<arr.length;i++)
{
for(int j=i;j<arr[i].length;j++)
{
int temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}

for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

}
}```