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

Leave a Reply

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