Using the modulo operator with negative numbers gives you a different result than you might think.
1 % 2 == 12 % 2 == 0-2 % 2 == 0-1 % 2 == -1
To get the result you want, you can replace your modulo tests with num % 2 == 0
and num % 2 != 0
.
Using the modulo operator with negative numbers gives you a different result than you might think.
1 % 2 == 12 % 2 == 0-2 % 2 == 0-1 % 2 == -1
To get the result you want, you can replace your modulo tests with num % 2 == 0
and num % 2 != 0
.