-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransposeOfMatrix.java
More file actions
41 lines (41 loc) · 1.36 KB
/
Copy pathtransposeOfMatrix.java
File metadata and controls
41 lines (41 loc) · 1.36 KB
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
//Question: Take a matrix and print transpose of the matrix.
package twoDArray;
import java.util.Scanner;
public class transposeOfMatrix {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of rows: ");
int m=sc.nextInt();
System.out.print("Enter number of columns: ");
int n=sc.nextInt();
int [][] arr=new int[m][n];
System.out.println("Enter the elements of the matrix: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j]=sc.nextInt();
}
}
//Method 1: Making a new matrix ->
// int [][] transpose=new int[n][m];
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// transpose[i][j]=arr[j][i];
// }
// }
// System.out.println("Transpose of the matrix is: ");
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// System.out.print(transpose[i][j]+" ");
// }
// System.out.println();
// }
//Method 2: changing the same matrix -> (Column wise printing)
System.out.println("Transpose of the matrix is: ");
for (int j = 0; j < n; j++) { // Columns
for (int i = 0; i < m; i++) { // Rows
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}