100 days of python Exercises

1 min read
Day 12: Python exercise that involves calculating the factorial of a number using a loop.
def factorial(n): if n < 0:
return "Factorial is not defined for negative numbers."
result = 1
for i in range(1, n + 1):
result *= i return result
Test the factorial function
number = int(input("Enter a non-negative integer: "))
print(f"The factorial of {number} is {factorial(number)}")
0
Subscribe to my newsletter
Read articles from Bright Andoh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
