How to Calculate the Factorial of a Number
In Python, the factorial of a number can be calculated using a loop or the built-in math.factorial()
function.
Example: Calculating Factorial Using a Loop
# Calculating factorial using a loop
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}.")
Output
The factorial of 5 is 120.
Explanation: A loop is used to multiply each integer from 1 to num
to get the factorial.