-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixinspiralform.java
More file actions
46 lines (39 loc) · 913 Bytes
/
matrixinspiralform.java
File metadata and controls
46 lines (39 loc) · 913 Bytes
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
package June19;
public class matrixinspiralform {
public static void main(String[] args) {
int one[][]= {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};
int count=0;
int rs=0,cs=0;
int re=one.length-1,ce=one[0].length-1;
while(count<(one.length*one[0].length)) {
for(int j=rs;j<=re;j++) {
if(count<(one.length*one[0].length)) {
System.out.print(one[j][cs]+" ");
count++;
}
}
cs++;
for(int j=cs;j<=ce;j++) {
if(count<(one.length*one[0].length)) {
System.out.print(one[re][j]+" ");
count++;
}
}
re--;
for(int j=re;j>=rs;j--) {
if(count<(one.length*one[0].length)) {
System.out.print(one[j][ce]+" ");
count++;
}
}
ce--;
for(int j=ce;j>=cs;j--) {
if(count<(one.length*one[0].length)) {
System.out.print(one[rs][j]+" ");
count++;
}
}
rs++;
}
}
}