The following code snippet does not test oddity correctly:
public static boolean isOdd(int i) { return i % 2 == 1; }
I read in the web that I should do it the following way:
public static boolean isOdd(int i) { return i % 2 != 0; }
Why is this?
Might be because (i % 2) != 0
works for both positive and negative numbers
Because when i
is negative --> (-1) % 2 == -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With