Menu
Coddy logo textTech

Recap - Dynamic Input

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 54 of 93.

challenge icon

Challenge

Medium

Read integers from input one at a time. Keep reading and summing the numbers until you encounter the value 0, which signals the end of input.

After reading 0, print the sum of all numbers entered before it, followed by the count of numbers (excluding the 0).

Use a while loop that continues until the sentinel value 0 is entered.

Print the output in this exact format:

Sum: [sum]
Count: [count]

For example, if the inputs are:

5
10
3
0

The output should be:

Sum: 18
Count: 3

If the first input is 0, the output should be:

Sum: 0
Count: 0

Try it yourself

fun main() {
    var sum = 0
    var count = 0
    
    // TODO: Write your code here
    // Use a while loop to read integers until you encounter 0
    // Keep track of the sum and count of numbers (excluding 0)
    
    println("Sum: $sum")
    println("Count: $count")
}

All lessons in Fundamentals

Practice on your own: Kotlin playground