Question

In: Computer Science

Using Java Prime numbers. Write a program that prompts the user for an integer and then...

Using Java

Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method reads a user input, constructs a PrimeGenerator object, and prints the primes. Submit the Java file and screenshot of the output in zip file.

Solutions

Expert Solution

package october;

import java.util.Scanner;

public class PrimeGenerator {
   //class variables
   private int limit; //will store the limit till when primes are to be generated
   private int start = 2; // will start from 2
  
   //constructor to initialise
   public PrimeGenerator(int limit) {
       this.limit = limit;
   }
  
   //method to print all the primes
   public void printPrimes() {
       System.out.println("The primes upto " + limit + " are: ");
      
       //loop will iterate until lower limit is less than upper limit
       while(start <= limit){
           //this willl get the immediate next prime.
           int p = nextPrime();   
           System.out.print(p + " ");
       }
   }
  
   //this ,ethod will give the next prime
   private int nextPrime() {
       //if start > limit then exit the program
       if(start > limit){
           System.exit(0);
       }
       //checking if start is prime or not
       if(isPrime(start) > 0) {
           //inc. start for the next iteration
           start++;
           return start-1;
       }
       else {
           //if start is not prime
           //inc. it by 1
           start++;
           //calulating the next prime
           return nextPrime();
       }
   }
  
   //method to check whether i is prime or not
   private int isPrime(int i) {
       if(i == 2) return i;
       if(i == 3) return i;
      
       for(int j = 2 ; j <= Math.sqrt(i) ; j++){
           if(i % j == 0) return 0;
       }
       return i;
   }
  
   public static void main(String[] args) {
       System.out.println("Enter the number till you want prime numbers: ");
       Scanner sc = new Scanner(System.in);
      
       int n = sc.nextInt();
       PrimeGenerator pg = new PrimeGenerator(n);
       pg.printPrimes();
       sc.close();
   }
  
}

if there is anything that you do not understand or need more help then please mention it in the comments section.


Related Solutions

Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT