-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay26.java
More file actions
72 lines (72 loc) · 2.08 KB
/
Copy pathDay26.java
File metadata and controls
72 lines (72 loc) · 2.08 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
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
/* height = height of the tower
tower[] = tower containing chips;
//Special Case
if the below chip has the same value as that of the top chip it'll fall(Removed) too
due to gravity!!!
*/
import java.util.Scanner;
public class Day26{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the height(size) of the tower");
int height = in.nextInt();
System.out.println("Enter the "+height+" default chips to put in the tower");
int tower[] = new int[height];
int index = 0;
for (index=0;index<height ;index++ ) {
tower[index] = in.nextInt();
}
boolean flag = true;
while(flag){
System.out.println("\n1.Put the chip on the tower");
System.out.println("2.Remove the chip from the top of the tower");
System.out.println("3.Display the tower");
System.out.println("4.Stop the program");
System.out.println("What's Your choice??");
int ch = in.nextInt();
switch(ch){
case 1: if (index>tower.length-1) {
System.out.println("The tower is full!!");
}else{
System.out.println("The value of the chip you want to enter!");
tower[index] = in.nextInt();
System.out.println(tower[index]+" is added to the tower");
index++;
}
break;
case 2: if (index<=0) {
System.out.println("The tower is empty");
}else{
index--;
int temp = tower[index];
tower[index] = 0;
System.out.println(temp+" was removed from the tower");
if (index!=0) {
while(tower[index-1]==temp){
index--;
tower[index] = 0;
System.out.println(temp+" fell from the tower due to Gravity!!");
}
}
}
break;
case 3: if (index<=0) {
System.out.println("The tower is empty");
}else{
System.out.println("Tower (bottom->top)");
for (int i=0;i<height ;i++ ) {
if (tower[i]!=0) {
System.out.print(tower[i]+" ");
}
}
System.out.println();
}
break;
case 4: flag = false;
break;
default: System.out.println("Your input is incorrect");
break;
}
}
}
}