M.Sc Python Programming Assignments
Assignment 17
Question:
Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
number = int(input("Enter a non-negative integer: "))
result = factorial(number)
print("Factorial:", result)
Output:
Enter a non-negative integer: 5
Factorial: 120