Recursion function in python

 Recursion function:

  • It is one of the most powerful tools in the programming language.
  • It is a process where function calls itself again and again.
  • Recursion basically divides the big problem into small problems up to the point where it can be solved easily.

Condition for Implementing Recursion:

  • It must contain base condition i.e. at which point recursion will end Otherwise it will become infinite.
  • It takes more memory as compare to LOOP statement because with every recursion call memory space is allocated for local variables.
  • It is less efficient in terms of speed and execution time
  • Suitable for complex data structure problems like TREE, GRAPH, etc

Example program:

  1. def factorial(num):

        if num==1:

            return 1

        else:

            return num*factorial(num-1)

    n=int(input("Enter any number : "))

    fact=factorial(n)

    print("Factorial of ",n," is ",fact)

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me