Recap - Dynamic Input
Part of the Fundamentals section of Coddy's Swift journey — lesson 50 of 86.
Challenge
EasyRead 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
whileloop withreadLine()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
endThe positive numbers are 3, 4, and 2. The product is 3 * 4 * 2 = 24, so the output should be:
24Try 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input