Recap - Factorial
Part of the Fundamentals section of Coddy's Ruby journey — lesson 43 of 88.
Challenge
EasyRead a number n from input. Calculate the factorial of n using a loop and print the result.
Remember that the factorial of n (written as n!) is the product of all positive integers from 1 to n.
The input will be a single positive integer.
For example, if the input is 5, the output should be:
120If the input is 7, the output should be:
5040Try it yourself
# Read input
n = gets.to_i
# TODO: Write your code below to calculate the factorial of n
# Output the result
puts factorialAll 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 Input3Operators 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