Python Program to Print Fibonacci Series Using Recursion

The Fibonacci Series is a sequence of numbers in which each is the sum of the two preceding numbers. In simple terms, it is a series of numbers in which the following number is found by adding the two previous numbers. This tutorial will discuss writing a Python program to print the Fibonacci series using recursion.

What is Recursion?

Recursion is a process in which a function calls itself a subroutine. It is used in programming to solve problems that can be broken down into more minor, more straightforward problems. In the case of the Fibonacci series, we can use recursion to find the next number in the series by calling the function repeatedly until we reach the desired number.

How do you write a Python program to print a Fibonacci series using Recursion?

To write a Python program to print the Fibonacci series using recursion, we need to create a function that takes the number n as input and returns the nth number in the Fibonacci series. Here is the code:

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

# take input from the user
terms = int(input("How many terms? "))

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

In this code, we first define a function called Fibonacci that takes the number n as input. If the value of n is less than or equal to 1, we simply return n. Otherwise, we call the Fibonacci function recursively with the values n-1 and n-2 and add them together to get the next number in the series. We then use a for loop to print the first n numbers in the series.

Related: Python Fibonacci Series using for loop

What is the Fibonacci series?

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In mathematical terms, the Fibonacci sequence is defined by the recurrence relation:

F(n)=F(nāˆ’1)+F(nāˆ’2)F(n)=F(nāˆ’1)+F(nāˆ’2)

With initial conditions:

F(0)=0,F(1)=1F(0)=0,F(1)=1

So, the Fibonacci sequence typically begins with 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Each subsequent number is the sum of the two preceding ones. The sequence was introduced to the West in the book “Liber Abaci” by Leonardo of Pisa, an Italian mathematician, in 1202. The Fibonacci sequence has numerous applications in various fields, including mathematics, computer science, nature, and art. The ratio of consecutive Fibonacci numbers converges to the golden ratio, a mathematical constant approximately equal to 1.618.

Conclusion

This tutorial discussed using recursion to write a Python program to print the Fibonacci series. Recursion is a powerful tool in programming that can help us solve complex problems by breaking them down into more minor, more straightforward problems. The Fibonacci series is an excellent example of how recursion can be used to find the solution to a problem.