M.Sc Python Programming Assignments
Assignment 4
Question:
Write a program which can compute the factorial of a given number.
Code:
def compute_factorial(number):
factorial = 1
if number < 0:
return "Factorial is not defined for negative numbers."
elif number == 0:
return 1
else:
for i in range(1, number + 1):
factorial *= i
return factorial
number = int(input("Enter a number: "))
result = compute_factorial(number)
print(f"The factorial of {number} is: {result}")
Output:
Enter a number: 3
The factorial of 3 is: 6