Recursion in Python:-
If the function calls itself under function then it is called recursion.
every loop process has been implemented by recursion.
we can call the same function multiple times under function.
function xyz():
xyz()
where xyz is the function that will be called under xyz() which is called recursion.
If we want to perform the addition of range from 1 to num then we can use this program using the recursion process.
def fun(num):
if num ==0:
return num
else:
return num+fun(num-1)
x=fun(5)
print(x)
Assignment of Recursion?
1) WAP to calculate factorial using recursion?
2) WAP to display Fibonacci series using recursion?
0 1 1 2 3 5 8 13 21 .......
3) WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?
#WAP to calculate factorial using recursion?
ReplyDeletedef fun(num):
if num ==1:
return num
else:
return num*fun(num-1)
x=fun(5)
print(x)
#WAP to display Fibonacci series using recursion?
ReplyDeletedef fun(num,i,j):
if num==0:
exit(0)
else:
k=i+j
print(k)
i=j
j=k
return fun(num-1,i,j)
fun(10,-1,1)
#WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?
ReplyDeletedef recursion(k,n,a,b):
if(k<=n):
if(k%2==0):
a=a+k;
else:
b=b+k;
else:
print("Even no's sum is :",a,"Odd no's sum is :",b);
return 0
return recursion(k+1,n,a,b);
k=recursion(1,50,0,0);
#WAP to display Fibonacci series using recursion?
ReplyDeletedef fibo(n):
if n==1:
return 0
if n==2:
return 1
return fibo(n-1)+fibo(n-2)
n=int(input("enter number"))
for i in range(1,n+1):
print(fibo(i))
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .