Transpose Matrix in Java
Here you will get and learn the program code to Transpose Matrix in Java programming. In this program we will follow the following Transpose Matrix rule given bellow.
What is Matrix Transpose?
Matrix Transpose is a mathematical operation that is used to interchange the values of rows to the columns and the values of columns to the rows.
Transpose Matrix in java Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //transpose of matrix in java import java.util.Scanner; class TransposeMatrix { public static void main (String[] args) { int i,j,row,col; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns :"); row = in.nextInt(); col = in.nextInt(); int matrix[][] = new int[row][col]; int transpose[][] = new int[row][col]; System.out.println("Enter elements of matrix : "); for ( i = 0 ; i < row ; i++ ) for ( j = 0 ; j < col ; j++ ) matrix[i][j] = in.nextInt(); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { transpose[j][i]=matrix[i][j]; } } System.out.println("matrix after transpose"); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { System.out.print(transpose[i][j] + " "); } System.out.println(); } } } |
Output
Check out our other Java Programming Examples