Python Forum
factorial using recursive function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
factorial using recursive function
#1
Hello,
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))
Reply


Messages In This Thread
factorial using recursive function - by spalisetty06 - Aug-25-2020, 02:10 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:29 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:38 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:44 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:46 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:50 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 02:51 PM
RE: factorial using recursive function - by buran - Aug-25-2020, 03:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 636 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,399 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,593 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,090 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,833 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 2,001 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,319 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 78,528 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  Recursive function returns None, when True is expected akar 0 3,451 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial, repeating Aldiyar 4 2,882 Sep-01-2020, 05:22 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020