isPrime function A prime number is a number that is only evenly divisible by its
ID: 3763931 • Letter: I
Question
isPrime function
A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program.
TIP: 1. Recall that then % operator divides one number by another, and returns the remainder of the division.
2. Recursion can be applied to the solution.
Explanation / Answer
public static boolean isPrime(int number) { return Primes.isPrime(number); } public static void main(String[] args) { String message; int number; Scanner keyboard = new Scanner(System.in); // Get the tract in feet. System.out.print("Enter a number: "); number = keyboard.nextInt(); // Determine whether it is prime or not. if (isPrime(number)) { message = "is a prime number."; } else { message = " is not a prime number."; } System.out.println("The number " + number + message); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.