Write a program that takes a positive integer as input and prints its factorial. Write two separate functions, one that computes the factorial iteratively, and the other recursively.
# Python 3.9.5
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
def iterat_factorial(n):
num_fac = 1
for i in range(1, n+1):
num_fac *= i
return num_fac
def main():
n = int(input('Enter number: '))
print('Recursive factorial: ', recur_factorial(n))
print('Iterable factorial: ', iterat_factorial(n))
if __name__ == '__main__':
main()
Comments
Leave a comment