In: Computer Science
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if they want to run the program again.
You will need:
A scanner object to read:
The user’s numerical entry
The user’s string response
Variables to store:
The user’s entry (should be an integer)
The user’s response (should be a string)
The number of primes (this will help to determine how many numbers are printed before moving to the next line)
(Include additional variables as needed)
Several loops:A do…while loop to repeat the program. This loop will contain:
A while loop for input validation
A while loop to determine if the number is prime (this loop may contain a nested for loop to check primes from 2 up to the user’s entry)
An if statement to determine when to skip a line after 10 numbers has been printed
Lots of comments for documentation
Tip: There are many ways to solve this problem; some are simpler and others are more complex than the guidelines given here. That said, you do not have to stick to this guideline exactly, as long as the program does the tasks described in the first paragraph. You are welcome to solve this problem in any way you want, as long as you are able to thoroughly explain it in the comments.
Samples of the output are shown below:
Enter a number greater than 2: 17
The prime numbers from 2 to 17 are
2 3 5 7 11 13 17
Do you want to enter another number?
Enter yes or no: yes
Enter a number greater than 2: 25
The prime numbers from 2 to 25 are
2 3 5 7 11 13 17 19 23
Do you want to enter another number?
Enter yes or no: yes
Enter a number greater than 2: 40
The prime numbers from 2 to 40 are
2 3 5 7 11 13 17 19 23 29
31 37
Do you want to enter another number?
Enter yes or no: no
/*import java.util.*;
class PrimeNumberList {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);//scanner to
read input
boolean rerun = false;
while(!rerun){
int count =0;
System.out.println("Enter the number greater than 2: ");
int CheckNum = keyboard.nextInt();
if(CheckNum <0)
System.out.println("Please enter a valid
number");
else
System.out.println("The prime number from 2 to " + CheckNum);
for(int num = 2; num<=CheckNum; num++){
//print prime numbers only
if(isPrimeNum(num)){
System.out.print(num + " ");
count++;
}
if(count==10)
{
count=0;
System.out.println("");
}
}
System.out.println("\n Do you want to enter another
number?");
System.out.println("Enter yes or no:");
String resp = keyboard.next();
if(resp.equals("no")) {
rerun = true;
}
}
}
public static boolean isPrimeNum(int num){
for(int i=2; i<num; i++){
if(num%i == 0){
return false; //number is divisible by any number return
false
}
}
return true;
}
}*/
Try running below code.
import java.util.*;
class PrimeNumberList {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);//scanner to read
input
boolean rerun = false;
while(!rerun){
int count =0;
System.out.println("Enter the number greater than 2: ");
int CheckNum = keyboard.nextInt();
if(CheckNum <0)
System.out.println("Please enter a valid number");
else
System.out.println("The prime number from 2 to " + CheckNum);
for(int num = 2; num<=CheckNum; num++){
//print prime numbers only
if(isPrimeNum(num)){
System.out.print(num + " ");
count++;
}
if(count==10)
{
count=0;
System.out.println("");
}
}
System.out.println("\n Do you want to enter another
number?");
System.out.println("Enter 1(if yes) or 0(if no):");
int resp = keyboard.nextInt();
if(resp==0) {
rerun = true;
}
}
}
public static boolean isPrimeNum(int num){
for(int i=2; i<num; i++){
if(num%i == 0){
return false; //number is divisible by any number return
false
}
}
return true;
}
}