When a function calls itself, then this is called recursion.
These are some of the script which are illustrating the use of recursion.
Script1: Write an script to print natural numbers from 1 to n in descending order using recursion, the value of n will be entered by the user.
def printnatno(n):
if n==0:
return
else:
print(n)
printnatno(n-1)
n=int(input("Enter the value of n "))
printnatno(n)
These are some of the script which are illustrating the use of recursion.
Script1: Write an script to print natural numbers from 1 to n in descending order using recursion, the value of n will be entered by the user.
def printnatno(n):
if n==0:
return
else:
print(n)
printnatno(n-1)
n=int(input("Enter the value of n "))
printnatno(n)
Script2: Write a function to calculate the factorial of a number using recursion.
def calfact(n):
if n==0 or n==1:
return 1
else:
return n*calfact(n-1)
n=int(input("Enter the number "))
ans=calfact(n)
print("The factorial of {} is {}".format(n,ans))
Be First to Post Comment !
Post a Comment