Recap - Product Array
Part of the Fundamentals section of Coddy's Ruby journey — lesson 62 of 88.
Challenge
EasyRead a single line of input containing comma-separated integers (e.g., 3,5,2,4).
Split the input into an array of integers, then calculate the product of all elements by multiplying them together using a loop. Print the final product.
Remember to start your product variable at 1, not 0.
For example, if the input is 3,5,2,4, the output should be:
120If the input is 2,3,4, the output should be:
24If the input is 1,2,3,4,5, the output should be:
120If the input is 10,5, the output should be:
50Try it yourself
# Read the comma-separated input
input = gets.chomp
# Split the input into an array of integers
numbers = input.split(",").map(&:to_i)
# Initialize the product variable
product = 1
# TODO: Write your code below to calculate the product of all elements
# Output the result
puts productAll lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input11Arrays
Creating ArraysAccessing Array ElementsModifying ArraysArray MethodsRecap - Product ArrayRecap - Reversed ArrayArray Shortcuts3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False