RECURSION IN PYTHON

SAIROSHNI


Recursion

  • Recursion can be defined as the process of defining something in terms of itself.
  • It is a process in which a function calls itself directly or indirectly.
  • A complicated function can be split down into smaller sub-problems utilizing recursion.
  • Recursion functions are challenging to debug.
  • A lot of memory and time is taken for expensive uses.

Program:


  #factorial using recursion
  print("program to calculate factorial of a number")
  number=int(input("enter the number"))
  def fact(number):
      if(number<1):
          print("enter only positive values")
          exit()
      elif(number==1):
          return number
      else:
          return number*fact(number-1)
  print(fact(number))


Output:


  program to calculate factorial of a number
  enter the number5
  120






More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send