التخطي إلى المحتوى الرئيسي

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?

تعليقات

  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)

    ردحذف
  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)

    ردحذف
  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);

    ردحذف
  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))

    ردحذف
  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)

    ردحذف
  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));

    ردحذف
  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]);

    ردحذف
  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)




















    ردحذف
  9. def fun(num):
    if num==1:
    return num
    else:
    return num*fun(num-1)

    x=fun(8)
    print(x)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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))

    ردحذف
  15. #square cube
    def fun(n):
    print(n*n, n*n*n)
    if (n):
    return 1
    return fun(n)
    fun(5)

    ردحذف
  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")

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...