Start learning free

LESSON 74 OF 338 · CONDITIONAL STATEMENTS

Parity of Number in Go: Even or Odd with the % 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

    GO · CONDITIONAL STATEMENTS

    Parity Of Number in Go, explained

    Problem overview

    The function parity receives a non-negative integer n and returns the string "even" or "odd". A number is even when it is divisible by 2 and odd otherwise. The example is n = 12: 12 divides by 2 with nothing left over, so the function returns "even". For 13 it returns "odd".

    Approach

    Dividing a number by 2 either leaves no remainder or leaves a remainder of 1. Go's remainder operator % gives that remainder directly: 12 % 2 is 0 and 13 % 2 is 1. So the whole problem is one test, n % 2 == 0. If it is true the number is even; if not, it is odd.

    The check takes constant time and needs no loop or extra data.

    Step by step

    1. Write the if with the remainder test. The condition is n % 2 == 0. Two different operators are at work: % computes the remainder, and == compares that remainder with 0. Leave the body empty for the moment.
    2. Return "even" inside the block. The function's result type is string, so the value goes in double quotes.
    3. Return "odd" after the if. This line is only reached when the condition was false. An even number has already left the function through the first return, so no else is needed.

    The solution in Go

    func parity(n int) string {
        if n % 2 == 0 {
            return "even"
        }
        return "odd"
    }
    

    Go details to notice:

    • Early return replaces else. After a return inside the if, the code below runs only for the other case. Writing the final return "odd" without an else is the common Go style, and adding an else around it would behave the same.
    • The function must end with a return. Since the result type is string, the compiler requires every path to return. The last line guarantees that. Without it you would see a missing return error.
    • Operator precedence. % binds tighter than ==, so n % 2 == 0 means (n % 2) == 0 with no parentheses needed. Go's formatter would print it as n%2 == 0; both spellings are the same program.
    • Strings use double quotes. "even" is a string. Single quotes would make a rune, a different type.

    Another way to see it

    An integer is even exactly when its lowest binary digit is 0, so n&1 == 0 gives the same answers as n % 2 == 0, including for negative numbers: for 12, 0, -4 it is true, and for 13, 1, -3 it is false. The remainder form is the one to learn first because it says what you mean, divisibility by 2, in words anyone can read. The same % pattern also answers "is this divisible by 3?" or "by 10?" by changing the number.

    Edge cases and common mistakes

    • Zero. 0 % 2 is 0, so parity(0) returns "even", which is correct since 0 is divisible by 2.
    • One. 1 % 2 is 1, so the result is "odd".
    • Testing for oddness with n % 2 == 1. For the non-negative numbers in this lesson it works. In Go, though, the sign of % follows the dividend, so -3 % 2 is -1, not 1, and a test against 1 would call -3 even. Comparing against 0 is correct for every integer: parity(-3) returns "odd" because -1 is not 0.
    • Using / instead of %. / is integer division and gives the quotient: 12 / 2 is 6. It says nothing about divisibility.
    • Using = instead of ==. = assigns, and Go refuses an assignment as a condition.
    • Returning the wrong type. Returning true or a number won't compile, because the function promises a string.

    Testing

    • parity(12) returns "even".
    • parity(13) returns "odd".
    • parity(0) returns "even".
    • parity(1) returns "odd".
    • parity(-3) returns "odd", even though the lesson only promises non-negative input.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free