In: Computer Science
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending order. Use a stack to store the prime numbers.
CODE IN JAVA:
import java.util.* ;
public class DemoStack {
public static void main(String[] args) {
Stack<Integer> st = new
Stack<Integer>();
int count = 0 ;
int i = 2 ;
boolean flag ;
while(count < 100) {
flag = true
;
for(int j = 2 ;
j < i ; j++) {
if(i % j == 0) {
flag = false ;
break;
}
}
if(flag) {
st.push(i);
count += 1 ;
}
i += 1 ;
}
while(!st.isEmpty()) {
System.out.println(st.pop());
}
}
}