Start learning free

LESSON 75 OF 415 · CONDITIONAL STATEMENTS

Parity of Number in Swift: Even or Odd with %

Practice Read the full explanation

Given a non-negative integer n, check its 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

    SWIFT · CONDITIONAL STATEMENTS

    Parity Of Number in Swift, explained

    Problem overview

    The method parity takes 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. For n = 12 the method returns "even", because 12 divided by 2 leaves nothing over.

    Approach: the remainder operator

    Swift's remainder operator % gives what is left over after a division. 12 % 2 is 0 and 13 % 2 is 1. A number is even exactly when dividing it by 2 leaves a remainder of 0, so the test is n % 2 == 0.

    No second test is needed for odd numbers. If a number isn't even, it must be odd, so the method can return "even" when the test succeeds and "odd" in every other case.

    Step by step

    1. Write the if. The condition is n % 2 == 0. Read it as "the remainder of n divided by 2 equals 0". The body can be empty at first.
    2. Return "even" inside the if. The return ends the method immediately.
    3. Return "odd" after the if. This line is reached only when the if condition was false, because an even number has already left the method. That is why no else is needed.

    The solution in Swift

    class Solution {
        func parity(_ n: Int) -> String {
            if n % 2 == 0 {
                return "even"
            }
            return "odd"
        }
    }
    

    Swift details worth knowing:

    • Operator precedence. % binds tighter than ==, so n % 2 == 0 means (n % 2) == 0 and needs no parentheses.
    • Return type. -> String means every path must return a string. The last line guarantees that, since it returns "odd" whenever the if doesn't.
    • Calling it. After let s = Solution(), s.parity(12) returns "even" and s.parity(13) returns "odd".
    • Two equal signs. The condition compares with ==. A single = would be an assignment, and Swift rejects it inside an if.
    • A built-in alternative. n.isMultiple(of: 2) is a method on integers that returns true for 4 and false for 7. It reads closer to the idea, but this lesson practises the remainder operator.

    Two equivalent shapes

    The same logic can be written with an explicit else:

    if n % 2 == 0 {
        return "even"
    } else {
        return "odd"
    }
    

    Both versions behave identically for every input. The first has one less level of nesting, which is why the lesson uses it. Because an if that returns leaves the method, the lines below it act as the "otherwise" part without any extra keyword.

    It also helps to see why, for a non-negative number, the remainder by 2 can only be 0 or 1. Every integer is either an exact multiple of 2 or one more than a multiple of 2, so there is no third case to handle. The method has exactly two outcomes, and one comparison is enough to separate them.

    Edge cases and common mistakes

    • Zero. 0 % 2 is 0, so parity(0) returns "even". That agrees with the definition: zero is divisible by 2.
    • One. 1 % 2 is 1, so the method returns "odd".
    • Negative numbers. The task promises n is non-negative, but the choice of test matters if you reuse the method. In Swift the remainder takes the sign of the left operand, so -3 % 2 is -1, not 1. A version that tests n % 2 == 1 would call -3 even. The test against 0 works for negative numbers as well, and s.parity(-3) returns "odd".
    • Typing "Even" or "ODD". The returned strings must match exactly, in lowercase.
    • Dividing instead of taking the remainder. n / 2 is integer division, so 7 / 2 is 3. It doesn't tell you whether anything was left over.

    Testing

    • parity(12) returns "even".
    • parity(13) returns "odd".
    • parity(0) returns "even".
    • parity(1) returns "odd".
    • parity(1000000) returns "even".
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free