Start learning free

LESSON 128 OF 348 · LOOPS & CONDITIONALS

Print Odd Numbers in Kotlin: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a number n as argument and prints to the console the odd numbers from 0 through n.

So when we called printOddNumbers(8), we expected our code to print:

1
3
5
7

but it seems like we made some mistakes because when we run our code, it prints nothing.


Assignment:

Your task is to fix our function such that it prints the odd numbers from 0 through n.

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · LOOPS & CONDITIONALS

    Buggy Code Print Odd Numbers in Kotlin, explained

    What the code is supposed to do

    The function printOddNumbers takes an Int named n and prints the odd numbers below it, one per line. Called with 8, it should print 1, 3, 5 and 7. The code you start with prints nothing at all:

    fun printOddNumbers(n: Int) {
        for (i in 0 until n) {
            if (n % 2 == 1) {
                println(n)
            }
        }
    }
    

    Notice that the loop covers 0 until n, so it stops at n - 1. The number n itself is never printed, which is why the expected output for 8 ends at 7. For printOddNumbers(9) the last line is also 7, not 9.

    What goes wrong

    The loop is fine: it makes i take the values 0, 1, 2, ..., n - 1. The mistake is in the two lines inside it, which both use n instead of i.

    • The test uses n. The parameter never changes while the function runs, so n % 2 == 1 has the same answer on every pass. For n = 8 the remainder is 0, the test is always false, and nothing is printed.
    • The print uses n. Even if the test passed, the line would print n again and again instead of the current number.

    Because the test only depends on n, the function behaves in one of two ways. For an even n it prints nothing. For an odd n it prints n itself, n times. With n = 3 the output is 3 three times:

    3
    3
    3
    

    Neither behaviour has anything to do with the odd numbers below n.

    Fixing it step by step

    1. Find the variable that changes. Each pass of the loop gives i a new value. The parameter n stays the same, so only i can tell one pass from another.
    2. Test i in the condition. Change n % 2 == 1 to i % 2 == 1. The remainder after dividing by 2 is 1 for odd numbers and 0 for even ones.
    3. Print i. Change println(n) to println(i) so that the number that passed the test is the one printed.
    4. Leave the loop and the rest alone. The range, the braces and mainFunction are already correct.

    The fixed code in Kotlin

    fun printOddNumbers(n: Int) {
        for (i in 0 until n) {
            if (i % 2 == 1) {
                println(i)
            }
        }
    }
    
    
    fun mainFunction() {
        printOddNumbers(8)
    }
    

    Some Kotlin details behind these lines:

    • until excludes its end. 0 until n runs from 0 to n - 1. The inclusive form is 0..n, which would include n as well. With 0..n and n = 9 the function would also print 9.
    • The loop variable is a val. In for (i in 0 until n), i is a fresh read-only value on every pass. You never write i++; the loop moves on by itself.
    • % is the remainder operator. For non-negative numbers, i % 2 is 0 when i is even and 1 when it is odd. Here i is never negative, so == 1 is a safe test.
    • Negative numbers behave differently. In Kotlin the remainder takes the sign of the left operand, so -3 % 2 is -1, not 1. If you ever need to test negative values, compare with != 0 instead of == 1.
    • Parameter names matter. Parameters are read-only values that stay the same during a call. When a loop variable and a parameter both hold numbers, it's easy to reach for the wrong name, and the compiler accepts either.

    Edge cases and common mistakes

    • n = 0 or n = 1. The loop covers 0 only, or nothing at all, and neither is odd, so the function prints nothing. That is the correct output.
    • n = 2. The loop covers 0 and 1, so the output is a single line, 1.
    • Odd n. printOddNumbers(9) prints 1, 3, 5, 7. The number 9 is odd but not below 9, so it isn't printed.
    • Fixing only one line. Changing the test but not the print prints the wrong number: for n = 8 it prints 8 four times, once for each odd i. Changing the print but not the test still prints nothing for an even n.
    • Testing for the wrong remainder. i % 2 == 0 selects even numbers, so it would print 0, 2, 4, 6.

    When a loop prints nothing, or the same value repeatedly, check whether the code inside it uses the loop variable. Code that never mentions it can't react to the loop.

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free