Python Program to Display Fibonacci Sequence Using Recursion

Posted in

Python Program to Display Fibonacci Sequence Using Recursion

Vinay Khatri
Last updated on August 10, 2022

    In this article, we have provided a python source code which can display a Fibonacci Sequence using the recursive technique.

    Prerequisite Python Program to Display Fibonacci Sequence Using Recursion

    • Python Input, Output
    • Python Functions
    • Python Recursion

    Fibonacci Sequence: A Fibonacci sequence is an integer series which start from 0 and 1 and each next integer is the sum of its previous two integers. for instance 0, 1 , 1, 2, 3, 5, 8, 13, ..... is a Fibonacci series. Recursion: Recursion is a technique in which the function calls itself again and again till the base condition get satisfied.

    Steps:

    • Ask the user to enter a number, which represents the number of integers to display from the Fibonacci series.
    • Create a recursive function which acts as a loop and call the function again and again till we get the range entered by the user.

    Python Program to Display Fibonacci Sequence Using Recursion

    Python Code

    def fibo(n,first =0,second =1):
        if n<1:
            print("Done with Recursion")
        else:
            f=first
            print(f)
            s=second
            now= f+s
            f= second
            s= now
           
            #recursion
            fibo(n-1,f,s)  
    
    num = int(input("Enter how many terms you want to display from Fibonacci Sequence: "))
    fibo(num)

    Output 1:

    Enter how many terms you want to display from Fibonacci Sequence: 15
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    Done with Recursion

    Output 2:

    Enter how many terms you want to display from Fibonacci Sequence: 10
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    Done with Recursion

    People are also reading:

    Leave a Comment on this Post

    0 Comments