6-Program
6-Program
1. Using a Loop
def factorial_loop(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
factorial = 1
for i in range(2, n + 1):
factorial *= i
return factorial
# Input from the user
number = int(input("Enter a number: "))
print(f"The factorial of {number} is: {factorial_loop(number)}")
2. Using Recursion
def factorial_recursive(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
return n * factorial_recursive(n - 1)