You are missing a bracket. If you keep your code clean it will solve the problem.
Also in your if statement you are checking if number % 2 != 0
. If it does not equal to 0 then it could potentially be a prime number. So I changed it to number % 2 == 0
to fix the problem.
The other mistake is that you kept printing even after you knew that the number is not prime.
public static void isPrime(int number)
{
for ( int i = 2; i<Math.sqrt(number) + 1; i ++)
{
if ( number%i==0 ) {//If number is not a prime
//Will print that the number is not a prime once
System.out.println(number +" is not a prime number.");
return ; //Will exit the method
}
}
//If you reach this point then the number is prime.
System.out.println(number +" is a prime number.");
}
5
solved Determine if a number is prime [closed]