TechBeamers
    • Python
      • Python Basic
      • Python OOP
      • Python Quizzes
      • Python Examples
      • Python Selenium
      • Python Advanced
    • Java
      • Java Basic
      • Java Flow Control
      • Java OOP
      • Java Quiz
      • Java Interview
    • C
    • C#
    • SQL
    • MySQL
    • Selenium
      • Selenium Tutorial
      • TestNG Tutorials
      • Selenium Interview
      • Selenium Quiz
    • Testing
      • Software Testing
      • Manual Testing
      • Software Testing Quiz
      • Testing Interview Questions
    • Agile
    • More
      • Android
      • Angular
      • Linux
      • Web Dev
      • Best IDEs
      • Front-End Quiz
      • ShellScript Quiz
      • C++ Quiz
      • IOT
      • General
    TechBeamers
    Python Examples

    Python Program to Generate a Fibonacci Sequence Using Recursion

    Updated:September 16, 20232 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    In this sample program, you will learn how to generate a Fibonacci sequence using recursion in Python and show it using the print() function.

    To understand this demo program, you should have the basic Python programming knowledge. Also, you can refer our another post to generate a Fibonacci sequence using while loop.

    However, here we’ll use the following steps to produce a Fibonacci sequence using recursion.

    1. Get the length of the Fibonacci series as input from the user and keep it inside a variable.
    2. Send the length as a parameter to our recursive method which we named as the gen_seq().
    3. The function first checks if the length is lesser than or equal to 1.
    4. If the length is lesser or equal to 1, then it returns immediately.
    5. In other cases, it makes two adjoining recursive calls with arguments as (length-1) and (length-2) to the gen_seq() function.
    6. We are calling the recursive function inside a for loop which iterates to the length of the Fibonacci sequence and prints the result.

    Below is the sample code of the Python Program to evaluate the Fibonacci sequence using recursion.

    Generate a Fibonacci sequence Using Recursion

    You can use IDLE or any other Python IDE to create and execute the below program.

    # Program to generate the Fibonacci sequence using recursion
    
    def gen_seq(length):
        if(length <= 1):
            return length
        else:
            return (gen_seq(length-1) + gen_seq(length-2))
    
    length = int(input("Enter number of terms:"))
    
    print("Fibonacci sequence using Recursion :")
    for iter in range(length):
        print(gen_seq(iter))
    

    The output of the above code is as follows.

    Enter number of terms:10
    Fibonacci sequence using Recursion :
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    Previous ArticlePython Program : Generate a Fibonacci Sequence Using While
    Next Article Python Program to Convert Lists into a Dictionary
    Join Us!
    Loading
    Latest Tutorials

    7 IoT Trends to Watch in 2023

    September 23, 2023

    SQL Table Creation: The Missing Manual

    September 15, 2023

    Python Print() with Examples

    September 25, 2023

    String concatenation in Python Explained

    September 24, 2023
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    • Terms of Use
    © 2023 TechBeamers. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.