Question

In: Computer Science

(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending...

(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.

Solutions

Expert Solution

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());
       }

   }

}


Related Solutions

Write a program that accepts an integer as input and then displays all the prime numbers...
Write a program that accepts an integer as input and then displays all the prime numbers smaller than or equal to that number.
Use VB.net to create a loop that posts the first 100 prime numbers. Then, write code...
Use VB.net to create a loop that posts the first 100 prime numbers. Then, write code to confirm the numbers are prime.
Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
Lab 1 – Numbers in Descending Order Design an application that accepts 10 numbers and displays...
Lab 1 – Numbers in Descending Order Design an application that accepts 10 numbers and displays them in descending order For the programming problem, create the pseudocode and enter it below. Enter pseudocode here
Write and test a Python program to print a set of real numbers in descending order....
Write and test a Python program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in order from largest to smallest....
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT