What is a recursion function?
In a simple word, a function which calls itself is called a recursive function. It becomes useful when you solve a problem that uses divide and conquers method. You can divide a problem and solve it recursively.Since a recursive function call itself, we need to define some condition for it so that it will stop. Otherwise, the program will run to the infinity. Usually, the 'if' statement is used to terminate the recursion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n): | |
if n == 1: | |
return 1 | |
else: | |
return n*factorial(n-1) | |
print(factorial(eval(input('Enter n to find its factorial:')))) |