Skip to content

Commit d12ddb9

Browse files
authored
Add files via upload
1 parent 590db52 commit d12ddb9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ch_10;
2+
3+
import ch_10.exercise10_05.StackOfIntegers;
4+
5+
/**
6+
* *10.6 (Displaying the prime numbers) Write a program that displays all the prime
7+
* numbers less than 120 in decreasing order. Use the StackOfIntegers class
8+
* to store the prime numbers (e.g., 2, 3, 5, ...) and retrieve and display them in
9+
* reverse order.
10+
*/
11+
public class Exercise10_06 {
12+
public static void main(String[] args) {
13+
StackOfIntegers stackOfIntegers = new StackOfIntegers();
14+
StackOfIntegers reverseStack = new StackOfIntegers();
15+
int n = 120;
16+
for (int i = 2; i < n; i++) {
17+
if (checkPrime(i)) {
18+
stackOfIntegers.push(i);
19+
}
20+
}
21+
22+
System.out.println("All prime numbers less than 120, in reverse order: ");
23+
while (stackOfIntegers.getSize() > 1) {
24+
System.out.print(stackOfIntegers.pop() + ", ");
25+
}
26+
System.out.print(stackOfIntegers.pop());
27+
}
28+
29+
static boolean checkPrime(int num) {
30+
boolean prime = true;
31+
for (int f = 2; f * f <= num; f++) {
32+
if (num % f == 0) {
33+
prime = false;
34+
break;
35+
}
36+
}
37+
return prime;
38+
39+
}
40+
}

0 commit comments

Comments
 (0)