Python Program to Display Fibonacci Sequence Using Recursion – 2024

Hey guys, in this blog we will see a Python Program to Display Fibonacci Sequence Using Recursion.

Code

# Python Program to Display Fibonacci Sequence Using Recursion

def fibo_recur(n):
    if n <= 1:
        return n
    else:
        return(fibo_recur(n-1) + fibo_recur(n-2))

terms = int(input('How many terms -> '))

# check if the number of terms is valid
if terms <= 0:
    print("Plese enter a positive integer")
else:
    print("Fibonacci sequence:")
    for i in range(terms):
        print(fibo_recur(i))

Output

How many terms -> 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

Suppose we need to find Fibonacci(5).

The flow for printing Fibonacci Series using Recursion will be like the below:

fib(5) = fib(4) + fib(3)
fib(4) = fib(3) + fib(2)
fib(3) = fib(2) + fib(1)
fib(2) = fib(1) + fib(0)
fib(1) = 1

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

Your email address will not be published. Required fields are marked *