How to print the transpose of a matrix in Java?

public class Transpose
{
public static void main(String args[])
{
int m=2,n=3;
int a[][]={{1,2,3},{4,5,6}};
for(int j=0;j<=m-1;j++)//0 to 1
{
for(int i=0;i<=n-1;i++)//0 to 2
{
System.out.print(a[j][i]+"\t");//a[0-1][0-2]
}
System.out.println();
}
System.out.println("\n\n");
for(int j=0;j<=n-1;j++)//0 to 1
{
for(int i=0;i<=m-1;i++)//0 to 2
{
System.out.print(a[i][j]+"\t");//a[0-1][0-2]
}
System.out.println();
}
}
}

Output

1   2   3
4   5   6

1   4
2   5
3   6

Leave a Reply