asked 67.6k views
3 votes
Write a program that prompts a user for a positive integer, then use a function to calculate whether the integer is a prime number or not. The program should do the following:

- Continues looping and prompting the user for input until the user enters "exit", then exit the program.
- If the user hasn't entered 'exit', then pass that value to a function.
- The function should accept a single parameter and return a single message back to the program.
- The function should check that the value from the user is a positive integer, return a message to the user if it is any other value.
- use a for loop to check whether the value from the user is an integer. I recommend using the modulus operator '% to the whe evenly divisible by any potential factors. The number of loops should vary depending on value from the use...
- If the number is not prime, return a message stating that the value is not prime.
- if the number is prime, return a message stating that the value is prime.

asked
User Insa
by
8.5k points

1 Answer

2 votes

Here's a Java program that prompts the user for a positive integer, checks if it is a prime number, and continues looping until the user enters "exit":

```java

import java.util.Scanner;

public class PrimeNumberChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String input;

do {

System.out.print("Enter a positive integer (or 'exit' to quit): ");

input = scanner.nextLine();

if (!input.equalsIgnoreCase("exit")) {

try {

int number = Integer.parseInt(input);

String result = checkPrimeNumber(number);

System.out.println(result);

} catch (NumberFormatException e) {

System.out.println("Invalid input. Please enter a positive integer.");

}

System.out.println();

}

} while (!input.equalsIgnoreCase("exit"));

scanner.close();

}

public static String checkPrimeNumber(int number) {

if (number <= 1) {

return "The number is not prime.";

}

for (int i = 2; i <= Math.sqrt(number); i++) {

if (number % i == 0) {

return "The number is not prime.";

}

}

return "The number is prime.";

}

}

```

In this program, the `main` method prompts the user for a positive integer (or "exit" to quit) inside a `do-while` loop. If the user enters a value other than "exit", it attempts to parse the input as an integer using `Integer.parseInt()`. If the parsing is successful, it calls the `checkPrimeNumber` method to check if the number is prime and prints the corresponding message. If the parsing fails, it catches the `NumberFormatException` and informs the user about invalid input.

The `checkPrimeNumber` method takes an integer as input and checks if it is a prime number. It first checks if the number is less than or equal to 1, in which case it is not prime. Then, it uses a `for` loop to check if the number is divisible by any potential factors up to the square root of the number. If it is divisible, the method returns the message "The number is not prime." Otherwise, it returns the message "The number is prime."

The program continues to loop until the user enters "exit", at which point it exits the program.