<< Back to Blog


Fibonacci Sequence Using Recursion

September 20, 2022

Tags: Algorithms Recursion 

Share on:     twitter iconfacebook icon

The occurrence of the Fibonacci sequence in nature is so frequent that it cannot be considered a coincidence.


A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8 .... The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.


def recursion_fibonacci(n: int):
    """
    A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....  
    The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. 
    This means to say the nth term is the sum of (n-1)th and (n-2)th term.
    """
    if n <= 1:
	return n
    else:
        return(recursion_fibonacci(n-1) + recursion_fibonacci(n-2))

number = 5
if number <= 0:
    raise ValueError("Positive number only")

for i in range(number):
    print(recursion_fibonacci(i))

<< Back to Blog