Java Program to display/print the transpose of a given matrix. The following program to print the transpose of a matrix has been written in three different possible ways which have been shared here:
- Using For Loop
- Using While Loop
- Using Do-While Loop
The compiler has been added to the aforementioned, so that you can execute the programs, along with suitable examples and sample outputs.
The Transpose of a given matrix is an operator which flips it over its diagonal. The order of the matrix changes unless it is a square matrix. The transpose of a transpose of a matrix is the given matrix itself – Check to Add two matrices.
Using For Loop
1) Transpose matrix will formed by inter changing the rows and columns of the original matrix .
2) Read the row number and column number and insert the elements in to the array mat1[][] using two for loops. for i=0 to i<row. for j=0 to j<col. insert element at mat[i][j].
3) To transpose the matrix, row>col then n=row /row<col then n=col. for i= 0 to i < n. for j= i+1 to j < n. Interchange the elements a[i][j] to a[j][i] and vice versa.
4) Print the transpose matrix using two for loops. for i=0 to i < col. for j=0 to j< row. print mat[i][j].
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.util.Scanner; class TRANSMatrix { public static void main(String args[]) { int row, col,i,j,temp,n; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); if(row >col) n=row; else n= col; int mat1[][] = new int[n][n]; System.out.println("Enter the elements of matrix"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j < col ;j++ ) mat1[i][j] = in.nextInt(); } System.out.println("\n\nOriginal matrix:-"); for ( i= 0 ; i < row ; i++ ) { for ( j= 0 ; j <col;j++ ) System.out.print(mat1[i][j]+" "); System.out.println(); } for ( i= 0 ; i < n; i++ ) for ( j= i+1 ; j < n;j++ ) { temp=mat1[i][j] ; mat1[i][j] =mat1[j][i] ; mat1[j][i] =temp; } System.out.println("Transpose of matrix:-"); for ( i= 0 ; i < col ; i++ ) { for ( j= 0 ; j < row;j++ ) System.out.print(mat1[i][j]+" "); System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter the number of rows 2 Enter the number columns 3 Enter the elements of matrix 1 5 6 4 2 3 Original matrix:- 1 5 6 4 2 3 Transpose of matrix:- 1 4 5 2 6 3 |
Using While Loop
1) Insert the elements into the matrix mat1 using while loop.
a) The while loop iterates until i<row increases the i value. Inner loop iterates until the condition j<col is false. Insert the element at mat1[i][j]. Increase the j value.
2) Now We are finding the transpose of mat1[][]. If row>col then n=row otherwise n=col. while loop iterates until i<n. j=i+1. while loop iterates until j<n. interchange the elements mat1[j][i] to mat1[i][j] vise versa. increase the j value. increase the i value.
3) Print the transpose of a matrix using two while loops with conditions i < col, j < row.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import java.util.Scanner; class TRANSMatrix { public static void main(String args[]) { int row, col,i,j,temp,n; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); if(row >col) n=row; else n= col; int mat1[][] = new int[n][n]; System.out.println("Enter the elements of matrix"); i= 0 ; while(i < row) { j= 0 ; while(j < col) { mat1[i][j] = in.nextInt(); j++; } i++; } System.out.println("\n\nOriginal matrix:-"); i= 0 ; while(i < row) { j= 0 ; while(j < col) { System.out.print(mat1[i][j]+" "); j++; } System.out.println(); i++; } i= 0 ; while(i < n) { j= i+1 ; while(j < n) { temp=mat1[i][j] ; mat1[i][j] =mat1[j][i] ; mat1[j][i] =temp; j++; } i++; } System.out.println("Transpose of matrix:-"); i= 0 ; while( i < col) { j= 0 ; while(j < row) { System.out.print(mat1[i][j]+" "); j++; } System.out.println(); i++ ; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Enter the number of rows 2 Enter the number columns 3 Enter the elements of matrix 1 2 3 4 5 6 Original matrix:- 1 2 3 4 5 6 Transpose of matrix:- 1 4 2 5 3 6 |
Using Do-While Loop
1) To insert the elements into the matrix,
a) i=0 do loop
- j=0 do loop
- insert an element at mat1[i][j], increase the j value
- then check the condition. This loop iterates until j<col.
- increase the i value. Then check the condition i<row. This loop iterates until i<row.
2) To transpose the matrix- row>col then n=row else n=col. i=0. do loop j=i+1. do loop. interchange the elements mat1[i][j] to mat1[j][i], increase the j value. Check the condition j<n at while.
This loop iterates until i<n condition is false. Increase the i value. Then check the condition i<n at while. This loop iterates until condition i<n is false.
3) Print the transpose of the matrix using two do the loop where inner loop checks the condition j<row, outer loop checks the condition i<col.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import java.util.Scanner; class TRANSMatrix { public static void main(String args[]) { int row, col,i,j,temp,n; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows"); row = in.nextInt(); System.out.println("Enter the number columns"); col = in.nextInt(); if(row >col) n=row; else n= col; int mat1[][] = new int[n][n]; System.out.println("Enter the elements of matrix"); i= 0 ; do { j= 0 ; do { mat1[i][j] = in.nextInt(); j++; }while(j < col); i++; }while(i < row); System.out.println("\n\nOriginal matrix:-"); i= 0 ; do { j= 0 ; do { System.out.print(mat1[i][j]+" "); j++; }while(j < col); System.out.println(); i++; }while(i < row); i= 0 ; do { j= i +1; do { if(j<n) {temp=mat1[i][j] ; mat1[i][j] =mat1[j][i] ; mat1[j][i] =temp; } j++; }while(j < n); i++; }while(i < n); System.out.println("Transpose of matrix:-"); i= 0 ; do { j= 0 ; do { System.out.print(mat1[i][j]+" "); j++; }while(j < row); System.out.println(); i++ ; }while( i< col); } } |
More Java Programs: