Question

In: Computer Science

Write a program IN JAVA that asks the user for a number. The program should check...

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

Solutions

Expert Solution

/*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;
}
}


Related Solutions

Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT