Problem overview
You write a method Parity that takes a non-negative integer n and returns the string "even" if n is divisible by 2, and "odd" otherwise. For n = 12 the result is "even", because 12 divided by 2 leaves nothing over. For n = 13 it is "odd".
Approach
An integer is even when dividing it by 2 leaves remainder 0, and odd when the remainder is 1. C# computes the remainder with the % operator:
| Expression | Value |
|---|---|
12 % 2 | 0 |
13 % 2 | 1 |
0 % 2 | 0 |
So the whole problem is one test: n % 2 == 0. If it is true, return "even". In every other case the number is odd.
Note that the problem says the input is non-negative, which makes zero the only unusual value. Zero is even, and 0 % 2 is 0, so it needs no special handling.
You can also see why the remainder test works by listing the numbers: 0, 2, 4, 6 and so on leave remainder 0, while 1, 3, 5 and 7 leave remainder 1. The remainder of division by 2 can only ever be 0 or 1, so two outcomes cover every integer, and one comparison separates them.
Step by step
- Test for even. Write
if (n % 2 == 0)with an empty body. The remainder is computed first, then compared with zero using==. - Return "even" inside the block. A
returnends the method immediately, so nothing after it runs for even numbers. - Return "odd" after the block. Execution only reaches this line when the
ifcondition was false, since even numbers have already returned. That is why noelseis needed. (This is the early-return style from the previous lessons.)
The solution in C#
class Solution {
public string Parity(int n) {
if (n % 2 == 0) {
return "even";
}
return "odd";
}
}
C# details
- The return type is
string, written in lowercase. Both returns give back a string literal in double quotes. Single quotes are for single characters, so'even'does not compile. %is the remainder operator. With positive operands it behaves like the modulo in maths, which is all this problem needs.- Operator precedence helps you here:
%binds tighter than==, son % 2 == 0means(n % 2) == 0with no parentheses needed. - The method name is
Paritywith a capital P, and the parameter isn, matching the editor.
A common stylistic question is whether to write else after the first return. Both forms behave the same; the version above skips it because the if branch always returns.
Edge cases and common mistakes
- Zero.
0 % 2 == 0, soParity(0)returns"even". This is correct. - Testing for odd with
n % 2 == 1. That works for non-negative input, which is what this problem guarantees. With negative numbers C# gives a negative remainder, for example-3 % 2is-1, so the check would miss them. Testing for even with== 0works for negatives too, and it is the safer habit. - Using
/instead of%. Withintoperands,/is integer division and gives the quotient, not the remainder:13 / 2is6. - Using
=instead of==.n % 2 = 0is a compile error, because you cannot assign to an expression. - Forgetting the final return. If the method ended after the
ifblock, the compiler would reject it, since the case where the condition is false would return nothing.
Testing
Parity(12)returns"even".Parity(13)returns"odd".Parity(0)returns"even".Parity(1)returns"odd".