Loading...
Loading...
Loading...
A maths quiz app labels each number as even or odd, and prime or not prime.
Write a utility class NumberUtil with a private constructor and two static methods, boolean isEven(long n) and boolean isPrime(long n). Numbers below 2 are not prime. Negative numbers can appear too.
n EVEN or n ODD, followed by PRIME or NOT PRIME.Example
Input
4 7 10 1 2
Output
7 ODD PRIME 10 EVEN NOT PRIME 1 ODD NOT PRIME 2 EVEN PRIME
2 is the only even prime; 1 is not prime.
Try the problem first. Open one hint at a time.
A number is even exactly when n % 2 == 0; this also works for negatives.
To test a prime, try divisors from 2 while d * d <= n.
Return false straight away for any n below 2.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor