Question: Hello! I need help with a problem from 'Objects First With Java 5th Edition'. Exercise 4.33 Write a method isPrime(int n) that returns true if
Hello!
I need help with a problem from 'Objects First With Java 5th Edition'.
Exercise 4.33
Write a method isPrime(int n) that returns true if the parameter n is a prime number, and false if it is not. To implement the method, you can write a while loop that divides n by all numbers between 2 and (n1) and tests whether the division yields a whole number. You can write this test by using the modulo operator (%) to check whether the integer division leaves a remainder of 0.
My answer is:
public boolean isPrime(int n) { int divider = 2; while(divider <= n-1) { if(n % divider == 0) { return false; } divider++; } return true; } but my teacher gave me feedback and said:
"1 is not a prime, it's an exception."
I know it's a stupid question but how do I fix it?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
