Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Recursion in Python:-

If the function calls itself under function block then it is called recursion. every loop process has been implemented by recursion.


we can call the same function multiple times under function.


def xyz():

       xyz()


where xyz is the function that will be called under xyz() which is called recursion.


Now I am implementing another example:-


def xyz(num):

    print("number ",num)

    if num>10:

        return 1

    xyz(num+1)


xyz(1)

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?



9 Comments

POST Answer of Questions and ASK to Doubt

  1. #WAP to calculate factorial using recursion?

    def fun(num):
    if num ==1:
    return num
    else:
    return num*fun(num-1)
    x=fun(5)
    print(x)

    ReplyDelete
  2. #WAP to display Fibonacci series using recursion?

    def 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)

    ReplyDelete
  3. #WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?

    def 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);

    ReplyDelete
  4. #WAP to display Fibonacci series using recursion?


    def 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))

    ReplyDelete
  5. #Shivam Shukla
    #1) WAP to calculate factorial using recursion?
    def fact(k):
    if(k==1):
    return k;
    else:
    return k*fact(k-1)
    val=fact(5)
    print(val)

    ReplyDelete
  6. #Shivam Shukla
    #2) WAP to display Fibonacci series using recursion?
    #0 1 1 2 3 5 8 13 21 .......

    def fib(times,a,b,lst):
    k=a+b
    lst.append(k);
    if(times==1):
    return lst
    return fib(times-1,b,k,lst);

    times=int(input("how many number's haved input : "));
    a=-1; b=+1; lst=[];
    print(fib(times,a,b,lst));

    ReplyDelete
  7. #Shivam Shukla
    #3) WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?
    def calc(num,even,odd):
    if(num%2==0):
    even+=num;
    else:
    odd+=num;
    if(num==1):
    return(even,odd);
    return calc(num-1,even,odd);

    num=int(input("Give's a range! yaa i means 0 to where..... : "));
    even=0; odd=0;
    s=calc(num,even,odd);
    print(s)
    print("Even number's count is :",s[0]);
    print("Odd number's count is :",s[1]);

    ReplyDelete
  8. # WAP to calculate factorial using recursion?
    def fun(fac):
    if fac==1:
    return fac
    else:
    return fac*fun(fac-1)
    x=fun(5)
    print(x)

    #WAP to display Fibonacci series using recursion?
    def fibo(n):
    if n==1:
    return 0
    if n==2:
    return 1
    return (fibo(n-1)+fibo(n-2))

    n=int(input("Enter the number = "))
    for i in range (1,n+1):
    print(fibo(i))

    # WAP to find even numbers and the odd numbers using recursion?
    def evenodd(n):
    if n==0:
    return True
    if n==1:
    return False
    return evenodd(n-2)

    n=int(input("Enter the number = "))
    if evenodd(n):
    print(n,"even")
    else:
    print(n,"odd")

    #WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?
    def oddeven(i,n,a,b):
    if i<=n:
    if i%2==0:
    a=a+i
    else:
    b=b+i
    else:
    print("Even",a,"odd",b)
    return 0
    return oddeven(i+1,n,a,b)
    i=oddeven(1,50,0,0)




















    ReplyDelete
  9. def fun(num):
    if num==1:
    return num
    else:
    return num*fun(num-1)

    x=fun(8)
    print(x)

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post