Start learning free

LESSON 73 OF 348 · CONDITIONAL STATEMENTS

Parity of Number in Kotlin: Even or Odd with %

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

    KOTLIN · CONDITIONAL STATEMENTS

    Parity Of Number in Kotlin, explained

    Problem overview

    Given a non-negative integer n, the function parity(n: Int): String returns the string "even" if n is divisible by 2 and "odd" otherwise. For n = 12 the answer is "even", because 12 divided by 2 leaves nothing over. For n = 13 it is "odd".

    Approach

    "Divisible by 2" means "dividing by 2 leaves a remainder of 0". Kotlin has an operator for the remainder: the modulo operator %. So 12 % 2 is 0 and 13 % 2 is 1. Every integer gives one of those two remainders when divided by 2, which is why a single test is enough: check for remainder 0, and everything else must be odd.

    Step by step

    1. Check for even. Write an if whose condition is n % 2 == 0. The remainder comes first, then the comparison with ==. Leave the body empty for now.
    2. Return "even". Inside the if, return "even". The function ends here for every even input.
    3. Return "odd". After the if block, return "odd". This line is reached only when the if was false, because an even n has already returned. So no else is needed.

    The solution in Kotlin

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

    Details worth knowing:

    • % is the remainder operator. With integers it returns what is left after integer division. Here 13 / 2 is 6 and 13 % 2 is 1.
    • = versus ==. The test uses two equals signs. One equals sign would try to assign, which Kotlin does not allow inside a condition.
    • Precedence. % binds tighter than ==, so n % 2 == 0 means (n % 2) == 0 without extra parentheses.
    • A declared return type. The function says it returns a String, so both paths must end in a return of a string. The final return "odd" covers the path where the if doesn't run.

    Another way to write it

    Because Kotlin's if produces a value, the same function can be a single expression:

    fun parity(n: Int): String = if (n % 2 == 0) "even" else "odd"
    

    It returns even for 12 and for 0, and odd for 13. Both forms behave the same. The lesson's version, with an if followed by a plain return, is easier to follow while you are learning how the pieces fit together.

    When % is new, it helps to try a few values by hand. Dividing 7 by 2 leaves 1, so 7 % 2 is 1. Dividing 100 by 7 gives 14 with 2 left over, so 100 % 7 is 2. Dividing by 2 can only leave 0 or 1, and that is why the parity test needs just one comparison.

    Edge cases and common mistakes

    • Zero. 0 % 2 is 0, so parity(0) returns "even". Zero is even.
    • Testing for odd with n % 2 == 1. For the non-negative inputs of this lesson, this works. It breaks for negative numbers, because in Kotlin the remainder takes the sign of the left operand: -3 % 2 is -1, not 1. Testing for the remainder being zero and treating everything else as the other case avoids the problem.
    • Dividing instead of taking the remainder. n / 2 == 0 is true only for 0 and 1, since integer division gives the quotient. The remainder is what carries the parity.
    • Returning the wrong type. The checks expect the strings "even" and "odd", in lowercase and with quotes in the code.
    • Printing instead of returning. Calling println shows text on the console, but the function must hand the string back.

    Testing

    Try these inputs:

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