Start learning free

LESSON 135 OF 348 · LOOPS & GLOBAL VARIABLES

Sum of Numbers in Kotlin: Adding a Range with a for Loop

Lesson Read the full explanation

Use a for loop to compute the sum of all numbers from 14 to 73.

Once computed, print this sum to the console.

STEP 1 OF 4
    Ctrl + Enter to check

    KOTLIN · LOOPS & GLOBAL VARIABLES

    Sum Of Numbers in Kotlin, explained

    What the exercise asks for

    The task is to compute the sum of all whole numbers from 14 to 73, both ends included, using a for loop, and to print the result. It is a first example of an accumulator: a variable that starts at a known value and collects a little more on every pass of a loop.

    The answer is a single line of output, 2610. You can check it by hand. The range holds 60 numbers, and pairing the smallest with the largest gives 14 + 73 = 87, 15 + 72 = 87 and so on. Thirty pairs of 87 make 2,610.

    The building blocks

    Variables with var

    Kotlin has two keywords for declaring a variable. val creates a read-only name that can't be assigned again, and var creates one that can. An accumulator changes on every pass, so it must be a var:

    var sum = 0
    sum = sum + 5
    sum += 5
    

    After these lines sum is 10. The last line is shorthand for the one before: sum += 5 means sum = sum + 5. Kotlin works out the type from the initial value, so var sum = 0 is an Int and no type needs to be written.

    Ranges

    The expression 14..73 is a range that includes both 14 and 73. Kotlin has other forms:

    • 0 until 4 includes 0, 1, 2 and 3 but stops before the end value;
    • 73 downTo 70 counts down through 73, 72, 71 and 70;
    • 0..10 step 2 takes every second number.

    Choosing between .. and until is the most common source of off-by-one errors. Here the task says "from 14 to 73", so both ends belong in the sum, and .. is the right choice.

    The for loop

    for (i in 14..73) runs its body once for each number in the range, and i holds that number during the pass. The loop variable is read-only inside the body, and a new one is made for each pass, so you don't increment it yourself.

    The exercise step by step

    1. Declare the accumulator. var sum = 0 starts the total at zero, because nothing has been added yet.
    2. Write the loop. Iterate with i over 14..73 so that every number in the task's range is visited once.
    3. Add inside the loop. sum += i adds the current number to the running total.
    4. Print after the loop. The println goes after the closing brace of the loop. Then it runs once, when the total is complete.
    var sum = 0
    for (i in 14..73) {
        sum += i
    }
    println(sum)
    

    This prints:

    2610
    

    Common mistakes

    • Printing inside the loop. If println(sum) is inside the braces, the program prints sixty partial sums, starting with 14 and 29, and ending with the right answer on the last line. The output has to be one line.
    • Using val for the total. A read-only name can't be reassigned, so sum += i would be rejected by the compiler.
    • Using until. 14 until 73 leaves out 73 and gives 2537, which is 2610 minus 73.
    • Not starting from zero. Starting the total at any other number shifts the answer by that amount.
    • Writing the range backwards. 73..14 is an empty range in Kotlin, because .. only counts upward. The loop body never runs and the program prints 0. To count down, use downTo.
    • Overflow in bigger sums. An Int holds values up to 2,147,483,647 and wraps around silently beyond that. For small ranges like this one it doesn't matter, but a sum over a very large range needs a Long, declared as var sum = 0L.

    Key takeaways

    • An accumulator is a var initialised before the loop and updated inside it.
    • a..b includes both ends, while a until b excludes b.
    • Print once, after the loop, when you want only the final value.
    • x += y is a shorthand for x = x + y.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free