Question

Follow exactly all the instructions provided below:

Do not deviate from the instructions provided below or add any unnecessary code to program.

Pay close attention to the directions! Make absolutely certain that you are doing exactly what the assignment asks you to do without any more code required.

Keep the program's code as simple as possible and begineer friendly level with no advanced level Java concepts/skills.

Write a program 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: 1. A scanner object to read 1. The users numerical entry 2. The users string response 2. Variables to store: 1. The users entry (should be an integer) 2. The users response (should be a string) 3. 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) 3. Several loops 1. A do...while loop to repeat the program. This loop will contain: 1. A while loop for input validation 2. 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 users entry) An if statement to determine when to skip a line after 10 numbers has been printed 4. Lots of comments for documentation

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

import java.util.InputMismatchException;
import java.util.Scanner;

public class PrintPrimes {

   public static void main(String[] args) {
       //scanner object to read user data
       Scanner input=new Scanner(System.in);
      
       //variables to store
       int number; //variable to store the number
       String repeat=""; //variable to store whether user's wants to run the program or not
       int numOfPrimes=0; //variable to hold the count of primes
      
       //loop that repeats until user terminates the program
       do
       {
           //for each execution set numOfPrimes to 0
           numOfPrimes=0;
          
           //read the number until which user wants to print primes
           System.out.print("\nEnter a number: ");
          
           //to check if user input is valid or not place it in a try catch block
           try
           {
               //read the integer
               number=input.nextInt();
              
               System.out.print("\nPrime Numbers upto "+ number + " are: \n");

               //run a loop from 2 until the specified number is reached
               for(int i=2; i < number; i++)
               {
                   //declare a boolean variable that tells whether the number is prime or not
                   boolean isPrime = true;

                   //run a loop from 2 to i
                   for(int j=2; j < i ; j++)
                   {   
                       //check if i is divisible by any other number
                       if(i % j == 0)
                       {
                           //if divisible set is Prime to false and go to next number
                           isPrime = false;
                           break;
                       }
                   }
                   // print the number if its prime
                   if(isPrime)
                   {
                       System.out.print(i+" ");
                       numOfPrimes++; //increment the count of primes
                      
                       //if number of primes is divisible by 10 the break the line
                       if(numOfPrimes%10==0)
                           System.out.println();
                   }
               }
              
              
           }
           //catch the exception if user entered invalid input
           catch(InputMismatchException exception)
           {
           //if not a valid integer print error
           System.out.println("Invalid input. Try again...");
           }
          
           //ask if user wants to repeat the execution
           System.out.print("\n\nDo you want to run the program again (yes/no)? ");
           input.nextLine();
           //read the users choice
           repeat=input.nextLine();
          
       }while(repeat.toLowerCase().equals("yes")); //repeat the execution as long as user types yes
       System.out.print("\nProgram terminated..");
   }

}

OUTPUT:

Console terminated> PrintPrimes [Java Application] C:Program Files Javaljre1.8.0 73 Enter a number: 50 Prime Numbers upto 50

Add a comment
Know the answer?
Add Answer to:
Follow exactly all the instructions provided below: Do not deviate from the instructions provided below or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...

    Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS PLEASE EMAIL ME! This programming assignment will just be a small extension to programming assignment 1. In the first assignment each of the functions could only take a predefined number of inputs. This time the objective is to be able to complete these functions with any amount of inputs. The user will pass arguments through the command line (this means you will have to...

  • I need a C++ program like this please! and please follow all instructions! Thanks so much!...

    I need a C++ program like this please! and please follow all instructions! Thanks so much! A menu-driven program with 3 choices: Names Rearranger, Number Validator, or Exit Menu Option 1: Name Rearranger: 1. Input ONE STRING from the user containing first name, middle name, and last name. ie, "John Allen Smith". USE THE GETLINE function. (Not cin) 2. Loop through the string and validate for a-z or A-Z or . If the name has any other characters, then ask...

  • Problem: Create a program that will ask scientist in tracking the resistances of products from various...

    Problem: Create a program that will ask scientist in tracking the resistances of products from various batches. The resistances of good resistors should be between 3 and 3.5 inclusive. Your program will need to track the number of resistances that are too high (>3.5 ohms), the number of resistances that are too low (<3.0ohms) and calculate the average of the resistances that fall in the proper range (>=3 and <= 3.5). Your program should prompt the user to enter name...

  • Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

    Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...

  • Finish the following program which adds up all integers from 0 to the user's given number inclusively using a While Loop

    // Finish the following program which adds up all integers from 0 to// the user's given number inclusively using a While Loop. The total should be// assigned to the variable 'total'.#includeusing namespace std;int main() {int number;int total = 0;int counter = 0; //initialize the variable// user enters a numbercout << "Enter a positive integer to find the summation of ";cout << "all numbers from 0 to the given number up to 100." << endl;cin >> number;// check for invalid user...

  • Please write the following code in C++ for Data Structures class. Please follow all the instructions...

    Please write the following code in C++ for Data Structures class. Please follow all the instructions in the picture below. Please write the program with zero warnings. Description: Please write a short program, randomStream, that asks for the name of a file and a number, and produces a files satisfying the following conditions: The file name is the name given by the user (exactly--no added suffixes please) The number of lines in the file is exactly the same as the...

  • Use PYTHON3 to create a program that asks the user for their name, and then prints...

    Use PYTHON3 to create a program that asks the user for their name, and then prints their initials. You must create a function called getInitials() that takes the name string and prints out the initials. It must be case insensitive and the initials must be printed in upper case. The program must contain a main() and a function called getInitials(), implemented as described in the function header comment given below. (You should include this function header comment in your own...

  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT