Factorial is a mathematical concept represented by n! (pronounced factorial of n), which represents the product of all positive integers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
In Python, we can use several methods to calculate factorials. Here are some of the methods:
Method 1: Use a loop
This is the most basic method, we multiply from 1 to n through a loop.
def factorial(n):
result = 1
for i in range(1, n+