Start learning free

LESSON 127 OF 348 · LOOPS & CONDITIONALS

Print Even - Odd in Kotlin: for, if and else

Practice Read the full explanation

Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.

Example:

Input: n = 6
Output (console):
0
odd
2
odd
4
odd


Note:

You have to print to the console, your function shouldn't return anything.

STEP 1 OF 3
    Ctrl + Enter to check

    KOTLIN · LOOPS & CONDITIONALS

    Print Even Odd in Kotlin, explained

    Problem overview

    The function printEvenOdd(n: Int) takes a positive integer n. For every number from 0 to n - 1, in order, it prints the word odd if the number is odd, and the number itself if it is even. It prints and returns nothing.

    For n = 6 the numbers are 0 to 5, and the output is:

    0
    odd
    2
    odd
    4
    odd
    

    0, 2 and 4 are even, so they are printed as numbers. 1, 3 and 5 are odd, so each is replaced by the word.

    Approach: one loop, one two-way decision

    The task has two parts: visit every number from 0 to n - 1, and for each one choose between two outputs. A for loop covers the first part. An if with an else covers the second, because every number falls into exactly one of the two groups, odd or even.

    To decide, use the remainder operator %. Dividing a whole number by 2 leaves 1 for an odd number and 0 for an even one. So i % 2 == 1 asks "is it odd?" and the else branch handles everything else, the even numbers.

    Step by step

    1. Loop with for (i in 0 until n). The range 0 until n starts at 0 and leaves out n, so the last value is n - 1, as the task wants. Writing 0..n would visit n too and print one line too many.
    2. Inside the loop, test whether i is odd. Write if (i % 2 == 1) and print the string "odd" with println.
    3. Add an else branch that prints i. It runs when the condition is false, that is, when i is even. Because the two branches are exclusive, each number produces exactly one line.

    The solution in Kotlin

    class Solution {
        fun printEvenOdd(n: Int) {
            for (i in 0 until n) {
                if (i % 2 == 1) {
                    println("odd")
                } else {
                    println(i)
                }
            }
        }
    }
    

    Kotlin details worth knowing:

    • Quotes decide the output. println("odd") prints the word, while println(i) prints the value of i. The same function accepts both.
    • The loop variable is read-only. i belongs to the loop. You can read it freely, but not assign to it.
    • The comparison uses ==. A single = is assignment and is not allowed as a condition.
    • if is also an expression in Kotlin. You could write println(if (i % 2 == 1) "odd" else i), but that mixes a string and an integer in one expression, which makes the type less clear. The plain if and else statement is easier to read here.

    Complexity

    Time: O(n). The loop runs n times and each pass does constant work.

    Space: O(1). No data is stored; the output goes to the console.

    Edge cases and common mistakes

    • n equal to 1. The loop visits only 0, which is even, so the output is a single line: 0.
    • Zero comes first. It is even, so the first line is the number 0, not the word.
    • Off by one. 0..n prints for n as well. With n = 6 that adds a seventh line, 6. Use until.
    • Swapped branches. Printing the number when i % 2 == 1 and the word otherwise gives the opposite output.
    • Printing both lines. Without the else (a second plain if that always runs), odd numbers would print the word and then the number too.
    • Quoting the variable. println("i") prints the letter i.
    • Negative numbers. The task's n is positive and the loop only visits non-negative numbers, but note that in Kotlin -3 % 2 is -1, so the test % 2 == 1 would not recognise negative odd numbers. Testing evenness with == 0 works for all integers.

    Testing

    • n = 6 prints 0, odd, 2, odd, 4, odd.
    • n = 1 prints 0.
    • n = 2 prints 0 and odd.
    • n = 3 prints 0, odd, 2.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free