-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranposeOfMatrix.java
57 lines (46 loc) · 1.3 KB
/
TranposeOfMatrix.java
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
/* Read inputSTDIN. Print your output to STDOUT*/
import java.util.*;
import java.io.*;
public class TranposeOfMatrix {
public static void main(String args[] ) throws Exception {
//Write code here
Scanner sc=new Scanner(System.in);
System.out.print("Enter Row :");
int row=sc.nextInt();
System.out.print("Enter Column :");
int col=sc.nextInt();
System.out.print("Enter Value :");
// array intialised & declared
int[][] mMatrix= new int[row][col];
int r,c;
for(r=0;r<mMatrix.length;r++){
for(c=0;c<mMatrix[r].length;c++){
mMatrix[r][c]=sc.nextInt();
}
}
// printing input Matrix :
for(r=0;r<mMatrix.length;r++){
for(c=0;c<mMatrix[r].length;c++){
System.out.print(mMatrix[r][c]);
}
System.out.println();
}
// transpose matrix declared
int[][] mTranpose= new int[row][col];
for(r=0;r<mMatrix.length;r++){
for(c=0;c<mMatrix[r].length;c++){
mTranpose[c][r]=mMatrix[r][c];
}
}
System.out.println("Transpose :");
for(r=0;r<mMatrix.length;r++){
for(c=0;c<mMatrix[r].length;c++){
System.out.print(mTranpose[r][c]);
if (c >= 0 && c < mMatrix[r].length-1){
System.out.print(" ");
}
}
System.out.println();
}
}
}