Контрольные задания > How to write a function in Python to calculate the factorial of a number?
Вопрос:
How to write a function in Python to calculate the factorial of a number?
Ответ:
Here's the Python code to calculate the factorial of a number:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Example usage:
number = 5
result = factorial(number)
print(f"The factorial of {number} is {result}")