Write a program that prints out as many primes as required that are larger than
ID: 3547109 • Letter: W
Question
Write a program that prints out as many primes as required that are larger than a value (including starting number) given by a user.
Note that the starting number must be positive (i.e. program should accept only values that are greater than 0 otherwise, ask user again to enter a positive number).
The program should loop until the user enters an acceptable number.
Now, it prints the first prime (strictly) larger than the number. Ask the user if he wants to print the next prime, if so print it out. Repeat until the user does not want any more primes
Explanation / Answer
private static void isPrime(int i) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime) {
System.out.println(i);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.