Print the Factorial of a Given Number

Calculate and print the factorial of a number.

Logic BuildingBeginner
Logic Building
# Take n as input
n = int(input("Enter n: "))

# Calculate factorial
factorial = 1
for i in range(1, n + 1):
    factorial *= i

print(f"Factorial of {n} is {factorial}")

Output

Enter n: 5
Factorial of 5 is 120

Multiply numbers from 1 to n.

Key Concepts:

  • Initialize factorial to 1
  • Multiply by each number from 1 to n
  • 5! = 1 * 2 * 3 * 4 * 5 = 120