python recursive function code example
Example 1: recuursion python
def recursive_method(n):
if n == 1:
return 1
else:
return n * recursive_method(n-1)
num = int(input('enter num '))
print(recursive_method(num))
Example 2: recursion in python
def rec(num):
if num <= 1:
return 1
else:
return num + rec(num - 1)
print(rec(50))
Example 3: recursive function in python
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
result = x * factorial(x-1)
return ()
num = 3
print("The factorial of", num, "is", factorial(num))
Example 4: recursion python examples
def factorial_recursion(n):
if n == 1:
return n
else:
return n*factorial_recursion(n-1)
Example 5: python recursion example
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
Example 6: recursion in python
def yourFunction(arg):
if arg == 0:
yourFunction(arg - 1)
return arg