Write a java program to display transpose of a given matrix.- Core Java Slip6

 Write a java program to display transpose of a given matrix.- Java Slip6

 

import java.io.*;

public class Matrix{ 

public static void main(String args[]){ 

//creating a matrix 

int original[][]={{10,30,40},{20,40,30},{30,40,50}};   

   

//creating another matrix to store transpose of a matrix 

int transpose[][]=new int[3][3];  //3 rows and 3 columns 

   

//Code to transpose a matrix 

for(int i=0;i<3;i++){   

for(int j=0;j<3;j++){   

transpose[i][j]=original[j][i]; 

}   

}   

 

System.out.println("Printing Matrix without transpose:"); 

for(int i=0;i<3;i++){   

for(int j=0;j<3;j++){   

System.out.print(original[i][j]+" ");   

}   

System.out.println();//new line   

}   

System.out.println("Printing Matrix After Transpose:"); 

for(int i=0;i<3;i++){   

for(int j=0;j<3;j++){   

System.out.print(transpose[i][j]+" ");   

}   

System.out.println();//new line   

}   

}} 

 

 

Output

 

C:\Program Files\Java\jdk1.7.0_80\bin>javac Matrix.java

 

C:\Program Files\Java\jdk1.7.0_80\bin>java Matrix

Printing Matrix without transpose:

10 30 40

20 40 30

30 40 50

Printing Matrix After Transpose:

10 20 30

30 40 40

40 30 50

 

C:\Program Files\Java\jdk1.7.0_80\bin>