Re: Method returning boolean
if(num%1==0 && num%num==0)
num%1 means the remainder of num/1 which is 0.
num/num = 1 with remainder 0.
Always true unless perhaps num = 0;
Re: Method returning boolean
Quote:
Originally Posted by
javapenguin
if(num%1==0 && num%num==0)
num%1 means the remainder of num/1 which is 0.
num/num = 1 with remainder 0.
Always true unless perhaps num = 0;
I know that but, what part of the code do I change in order for it to make certain values to become "false" which is supposed to be false, whereas "true" is true.
Re: Method returning boolean
Ohhhh, non-prime numbers also would have that factor.
Have a for loop that starts at 3
Code java:
public static boolean isPrime(int num)
{
if (num <=1)
return false;
// if num = 2, it will skip the for loop and return true.
// but if for some reason it doesn't, then just change it to x = 3 and have condition
// if (num==2) return true;
for (int x =2; x < num; x++)
{
if (num%x ==0)
return false;
}
return true;
}