Menu
Coddy logo textTech

Recap - Factorial

Part of the Fundamentals section of Coddy's R journey — lesson 42 of 78.

challenge icon

Challenge

Easy

Read 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 = 120
  • 3! = 3 × 2 × 1 = 6
  • 1! = 1
  • 6! = 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