mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
rewrite the algorithm from scratch (#1351)
This commit is contained in:
parent
14c23bc847
commit
870eebf349
|
@ -1,19 +1,21 @@
|
||||||
"""Python program to find the factorial of a number provided by the user."""
|
def factorial(input_number: int) -> int:
|
||||||
|
"""
|
||||||
|
Non-recursive algorithm of finding factorial of the
|
||||||
|
input number.
|
||||||
|
>>> factorial(1)
|
||||||
|
1
|
||||||
|
>>> factorial(6)
|
||||||
|
720
|
||||||
|
>>> factorial(0)
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
|
||||||
# change the value for a different result
|
if input_number < 0:
|
||||||
NUM = 10
|
raise ValueError('Input input_number should be non-negative')
|
||||||
|
elif input_number == 0:
|
||||||
# uncomment to take input from the user
|
return 1
|
||||||
# num = int(input("Enter a number: "))
|
else:
|
||||||
|
result = 1
|
||||||
FACTORIAL = 1
|
for i in range(input_number):
|
||||||
|
result = result * (i + 1)
|
||||||
# check if the number is negative, positive or zero
|
return result
|
||||||
if NUM < 0:
|
|
||||||
print("Sorry, factorial does not exist for negative numbers")
|
|
||||||
elif NUM == 0:
|
|
||||||
print("The factorial of 0 is 1")
|
|
||||||
else:
|
|
||||||
for i in range(1, NUM + 1):
|
|
||||||
FACTORIAL = FACTORIAL * i
|
|
||||||
print("The factorial of", NUM, "is", FACTORIAL)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user