Recap - Factorial
Part of the Fundamentals section of Coddy's R journey — lesson 42 of 78.
Challenge
EasyRead a number n from input. Calculate and print the factorial of n using a loop.
The factorial of a number n (written as n!) is the product of all positive integers from 1 to n. For example:
5!= 5 × 4 × 3 × 2 × 1 = 1203!= 3 × 2 × 1 = 61!= 16!= 6 × 5 × 4 × 3 × 2 × 1 = 720.
Print the final result using print().
Try it yourself
# Read input
con <- file("stdin", "r")
n <- as.integer(suppressWarnings(readLines(con, n = 1)))
# Initialize result
result <- 1
# TODO: Write your code here to calculate the factorial using a loop
# Print the result
print(result)All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values