-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranspose.java
More file actions
41 lines (38 loc) · 1.18 KB
/
Transpose.java
File metadata and controls
41 lines (38 loc) · 1.18 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
import java.util.Scanner;
import java.util.Arrays;
public class Transpose {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows of the matrix : ");
int r=sc.nextInt();
System.out.println("Enter number of columns of the matrix : ");
int c=sc.nextInt();
int ar[][]=new int[r][c];
System.out.println("Enter elements (row-wise) in the 2D array : ");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
ar[i][j]=sc.nextInt();
}
}
System.out.println("Entered Matrix :");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
int tr[][]=new int[c][r];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
tr[j][i]=ar[i][j];
}
}
System.out.println("Answer Matrix : ");
for(int j=0;j<c;j++){
for(int i=0;i<r;i++){
System.out.print(tr[j][i]+" ");
}
System.out.println();
}
}
}