Recursion in Python:-

16
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?

Tags

Post a Comment

16Comments

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
  10. #WAP to calculate factorial using recursion?
    def fact(num):
    if num==1:
    return num
    else:
    return num*fact(num-1)
    n=fact(5)
    print(n)

    ReplyDelete
  11. #WAP to display Fibonacci series using recursion?
    def fib(num,a,b):
    if num==0:
    return
    else:
    c=a+b
    print(c)
    a=b
    b=c
    return fib(num-1,a,b)
    fib(15,-1,1)

    ReplyDelete
  12. #WAP to calculate the sum of even numbers and the odd numbers of 1 to 50 using recursion?
    def even(num):
    if num>0:
    even(num-1)
    print(2*num)

    even(25)

    ReplyDelete
  13. #WAP to calculate factorial using recursion?

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

    ReplyDelete
  14. #PROGRAM TO CHECK PRIME NUMBER USING RECURSION
    def prime(n, i=2):
    if n == i:
    return "prime number"
    elif n % i == 0:
    return "Not prime"
    return prime(n, i + 1)
    print(prime(5))

    ReplyDelete
  15. #square cube
    def fun(n):
    print(n*n, n*n*n)
    if (n):
    return 1
    return fun(n)
    fun(5)

    ReplyDelete
  16. #PROGRAM TO CHECK PRIME NUMBER USING RECURSION

    def prime(n, i=2):
    if n == i:
    return 1
    elif n % i == 0:
    return 0
    return prime(n, i + 1)
    n=int(input("Enter number"))
    s=prime(n)

    if s==1:
    print("Prime Number")

    if s==0:
    print("Not prime")

    ReplyDelete
Post a Comment