Python Program on Print Fibbonacci Series using Recursion
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will explore a Python program that generates the Fibonacci series up to a specified limit using recursion. The Fibonacci sequence is an ordered set of numbers, with each term being the summation of the two immediately preceding numbers, initially commencing from 0 and 1. This program demonstrates the concept of recursion and how it can be employed to calculate and display Fibonacci numbers.
Prerequisites
- Functions and their role in code organization.
- Recursion and its basic principles.
- Understanding and use of global variables.
- Proficiency in acquiring user input within Python programs.
Topic Explanation
This program centers on fibbo(n), using recursion to generate the Fibonacci series up to the nth term. Global variables ‘a’ and ‘b’ track series elements, and user input defines the series limit, invoking fibbo() to produce the series.
The program offers practical insights into recursion and global variables, enhancing comprehension. With user-defined limits and recursive methodology, it effectively demonstrates their role in solving mathematical problems, serving as an instructive hands-on exercise for readers.
Code:
# fibbonacci series 1 2 3 5 8 13 ......n using recursion
# Initializing the first two numbers of the Fibonacci series
a = 0
b = 1
# Defining the recursive function to generate Fibonacci series
def fibbo(n):
global a, b
# Base case: if n is 0, exit the recursion
if n == 0:
return
else:
# Calculate the next Fibonacci number
c = a + b
# Print the current Fibonacci number
print(c, end=" ")
# Update values of a and b
a = b
b = c
# It Recursively calls the function
fibbo(n - 1)
# Taking user input for the limit of the Fibonacci series
n = int(input("Enter the limit: "))
# Calling the Fibonacci function with the user-specified limit
fibbo(n)Output:
Enter the limit: 5
1 2 3 5 8
Code Explanation:
1. Initialize two global variables a and b with 0 and 1 – the first two Fibonacci numbers
2. Define a recursive function fibbo() that takes an integer parameter n
3. Base case:
- If n == 0, exit the recursion. This is the terminating case.
4. Recursive case:
- Calculate next Fibonacci number by adding previous two (a + b) and store in c
- Print the current number c
- Update a and b for next iteration (a becomes b, b becomes c)
- Call fibbo() again with n-1 to generate next number recursively
5. Take user input for the limit of the Fibonacci series to be generated
6. Call the fibbo() function with the user input limit
7. This recursively prints all Fibonacci numbers upto the limit
Summary
In conclusion, this Python program showcases the generation of the Fibonacci series up to a user-set limit. It employs recursion as a central technique. The article dissects the code for readers with basic Python knowledge, emphasizing the practical application of recursion, global variables, and user input. By demonstrating the use of recursion, the program illustrates an elegant and efficient approach to computing the Fibonacci sequence. This helps readers grasp these techniques for problem-solving and algorithm development. It ultimately enhances their proficiency in Python programming.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

