Skip to main content

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?

Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...