Menu
Coddy logo textTech

Recap - Dynamic Input

Part of the Fundamentals section of Coddy's Swift journey — lesson 50 of 86.

challenge icon

Challenge

Easy

Read numbers from input until the user enters "end". Calculate and print the product of all positive numbers entered.

You will receive the following inputs:

  • Multiple lines of input, each containing either a number or the word "end"
  • The input ends when "end" is entered

Requirements:

  • Use a while loop with readLine() to continuously read input
  • Stop reading when the input equals "end"
  • For each valid integer input, check if it's positive (greater than 0)
  • Multiply only the positive numbers together
  • Print the final product

Note: Start with a product of 1 (since multiplying by 1 doesn't change the result). If no positive numbers are entered, the output should be 1.

For example, if the inputs are:

3
-2
4
0
2
end

The positive numbers are 3, 4, and 2. The product is 3 * 4 * 2 = 24, so the output should be:

24

Try it yourself

var product = 1

// TODO: Write your code below
// Use a while loop with readLine() to read input until "end"
// For each positive number, multiply it to the product


print(product)

All lessons in Fundamentals