![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements