Answer to Question #292405 in Python for kalpana T

Question #292405

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. 


1
Expert's answer
2022-01-31T07:56:11-0500
# 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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS