Recap - Dynamic Input
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 54 of 93.
Challenge
MediumRead 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
0The output should be:
Sum: 18
Count: 3If the first input is 0, the output should be:
Sum: 0
Count: 0Try 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground