An even number divides by 2 with no remainder. An odd number leaves exactly one unit over. The question reduces to a single check: what is the remainder when you divide n by 2?
The modulo operator % gives you that remainder. Negative numbers complicate it. In Java, C++, C#, Go, JavaScript, and TypeScript, -3 % 2 evaluates to -1, not 1. Testing n % 2 == 1 then misclassifies every negative odd number as even. Comparing against zero with n % 2 != 0 avoids this, because -1 is just as "not zero" as 1.
n can be negative → Use n % 2 != 0 to detect odd numbers instead of n % 2 == 1, so negative odd values are handled correctly.n can be zero → Zero divided by 2 leaves no remainder, so it is even. The modulo check handles this without any special case.n fits in 32 bits → A plain int is enough. No overflow concerns and no need for wider types.Compute n % 2. If the remainder is 0, the number is even. Anything else means odd. Framing the test as "is the remainder zero" rather than "is the remainder one" keeps it correct for negative inputs, where an odd number's remainder can come out negative.
n divided by 2."Even"."Odd".Input:
Dividing -3 by 2 gives a remainder of -1. Since -1 is not 0, the number is odd. A test of n % 2 == 1 would fail here, because the remainder is -1, not 1. The comparison against 0 gives the right answer.
n.In binary, the last bit has place value 1, and every higher bit represents an even place value (2, 4, 8, and so on) that cannot change parity. So a number is odd when its last bit is set.
The expression n & 1 isolates that bit: it keeps the rightmost bit of n and clears the rest, giving 1 for odd numbers and 0 for even ones. This works for negatives too, since two's complement keeps the same parity bit at the bottom.
n & 1 to extract the lowest bit."Odd"."Even".Input:
The number 7 is 0111 in binary. Taking 7 & 1 lines up 0111 against 0001 and keeps only the last bit, giving 1. Since the result is 1, the number is odd.
n.