Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python Program to Find the Fibonacci Series Using Recursion



When it is required to find the Fibonacci sequence using the method of recursion, a method named ‘fibonacci_recursion’ is defined, that takes a value as parameter. It is called again and again by reducing the size of the input.

Below is a demonstration of the same:

Example

def fibonacci_recursion(my_val):
   if my_val <= 1:
      return my_val
   else:
      return(fibonacci_recursion(my_val-1) + fibonacci_recursion(my_val-2))
num_terms = 12
print("The number of terms is ")
print(num_terms)
if num_terms <= 0:
   print("Enter a positive integer...")
else:
   print("The Fibonacci sequence is :")
   for i in range(num_terms):
      print(fibonacci_recursion(i))

Output

The number of terms is
12
The Fibonacci sequence is :
0
1
1
2
3
5
8
13
21
34
55
89

Explanation

  • A method named ‘fibonacci_recursion’ is defined that takes a value as parameter.

  • The base conditions are defined.

  • The method is called again and again until the output is obtained.

  • Outside the method, the number of terms are defined and displayed on the console.

  • The numbers within the range are iterated, and the recursive method is called.

  • The relevant output is displayed on the console.

Updated on: 2021-09-07T10:59:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements