Hello,
I am practicing on factorial using Recursive function. I am facing an issue here. Kindly correct me.
I am practicing on factorial using Recursive function. I am facing an issue here. Kindly correct me.
'''Recursive Function''' def factorial(n): if n == 0: result = 1 else: result = n * factorial(n - 1) print("the factorial of {} is {}".format(n, result)) return result factorial = factorial(5)
Output:the factorial of 0 is 1
the factorial of 1 is 1
the factorial of 2 is 2
the factorial of 3 is 6
the factorial of 4 is 24
the factorial of 5 is 120
120
Sorry, I got it, I forgot to give factorial while calling. Once added, it is resolved. I have another question please. once it comes to result = n * factorial(n - 1), isn't it that, it should go to def factorial(n): again (the first line), why is it printing print("the factorial of {} is {}".format(n, result)). I hope you understand what I am trying to ask. What I meant is, under else statement, since it is factorial(n - 1), it is supposed to go function definition, why is it printing the next statement called print ("the factorial of {} is {}".format(n, result))