Start learning free

LESSON 72 OF 347 · CONDITIONAL STATEMENTS

Parity of Number in C#: Even or Odd with the Modulo Operator

Practice Read the full explanation

Given a non-negative integer n, check it's parity and return "even" or "odd"

A number is "even" if it is divisible by 2 and "odd" otherwise

Example:

Input: n = 12

Output: "even"

Explanation:
12 is divisible by 2, so it is even
Hints:

You can make use of modulo operator with an if statement

12 % 2 is 0, therefore 12 is "even"
13 % 2 is 1, therefore 13 is "odd"
STEP 1 OF 3
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    Parity Of Number in C#, explained

    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:

    ExpressionValue
    12 % 20
    13 % 21
    0 % 20

    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

    1. Test for even. Write if (n % 2 == 0) with an empty body. The remainder is computed first, then compared with zero using ==.
    2. Return "even" inside the block. A return ends the method immediately, so nothing after it runs for even numbers.
    3. Return "odd" after the block. Execution only reaches this line when the if condition was false, since even numbers have already returned. That is why no else is 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 ==, so n % 2 == 0 means (n % 2) == 0 with no parentheses needed.
    • The method name is Parity with a capital P, and the parameter is n, 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, so Parity(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 % 2 is -1, so the check would miss them. Testing for even with == 0 works for negatives too, and it is the safer habit.
    • Using / instead of %. With int operands, / is integer division and gives the quotient, not the remainder: 13 / 2 is 6.
    • Using = instead of ==. n % 2 = 0 is a compile error, because you cannot assign to an expression.
    • Forgetting the final return. If the method ended after the if block, 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".
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free