Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A prime number is a number that is only divisible by 1 and the number itself. Fo

ID: 3563971 • Letter: A

Question

A prime number is a number that is only divisible by 1 and the number itself. For example 5, 7, 11, 19 and 113 are prime numbers but 8 is not a prime number.

Write a program that given an integer value, n, displays the smallest prime number that is larger than n. The following are a few input/output examples:

Input Output

12       13

25        29

113       127

359        367

Your program must contain and use the following methods

--public static boolean isPrime(int m) = Accepts one integer parameter, m, and returns true, if m is a prime number. Otherwise, isPrime returns false.

For example, isPrime(72) returns false, whereas isPrime(11) returns true;

--public static int nextPrime(int m) Accepts one integer parameter, m and returns the smallest prime number that is larger than m. For example, nextPrime(6) will return 7 and nextPrime(97) returns 101

The main method must:

1. Prompt the user for a positive integer number with a meaningful message.

2. If the input is not a positive integer value, the user must be informed of the error in the input, and then be prompted for another input

3. If more than three consecutive erroneous input values are entered, the program must be terminated with an appropriate error message

4. If the input value, n, is valid, then the smallest prime number that is larger than n must be displayed

5. After displaying the result, the program must ask whether or not the user wants to enter another input (Do you want to continue? (y/n))

6. The user must answer either yes, no, y or n using any combination of lower and upper case letters.

7. If an invalid answer is given, the user must be informed that the answer was not acceptable, and then be prompted again for another answer.

8. If more than three consecutive erroneous answers are given, the program must be terminated with an appropriate error message.

9. If the answer in step 6, is Yes (in any allowed form) the program must continue from step 1.

10. If the answer is No (in any allowed form) the program must terminate with a message informing that the program is terminated normally.

11. All input must be done using an Scanner object i.e. Scanner inp = new Scanner (System.in);

12. All output should be done using System.out.

Explanation / Answer

Here is the solution

import java.util.Scanner;

public class prime {
   public static void main(String args[]) {
       // getting the scanner object
       Scanner sc = new Scanner(System.in);
       int chances = 0;
       // getting the number input
       do {
           int num = 0;
           for (int i = 0; i < 3; i++) {
               System.out.println("Please enter a positive number");
               num = sc.nextInt();
               if (num < 0) {
                   // checking if the number of attempts exceeds three
                   if (i == 2) {
                       // exiting the program if the user enters a negative
                       // number more than three times
                       System.out.println("Exiting the program");
                       System.exit(0);
                   }
                   System.out.println("The value entered is negative");
                   continue;
               } else {
                   break;
               }
           }
           // getting the next prime number
           int nextNum = nextPrime(num);
           System.out.println("The next prime number is:" + nextNum);
           while (chances < 3) {
               System.out.println("Do you want to continue?(y/n)");

               String ch = sc.next();
               if (ch.equals("y")) {
                   chances = 0;
                   break;
               } else if (ch.equals("n")) {
                   chances = 0;
                   System.exit(0);
               } else {
                   if (chances == 2) {
                       // exiting the program if the user enters an incorrect
                       // choice more than three times
                       System.out.println("Exited");
                       System.exit(0);
                   }
                   System.out.println("Wrong choice entered");
                   chances += 1;
               }
           }
       } while (true);
   }

   public static boolean isPrime(int n) {
       int flag = 0;
       for (int i = 2; i < n; i++) {
           if (n % i == 0) {
               flag = 1;
           }
       }
       if (flag == 0)
           return true;
       else
           return false;
   }

   public static int nextPrime(int m) {
       int counter = 0;
       int number = m + 1;
       while (counter == 0) {
           // checking if the number is prime using the isPrime function
           if (isPrime(number)) {
               counter += 1;
           } else {
               number += 1;
           }
       }
       return number;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote