In this tutorial we are going to learn how to print Fibonacci series in python program using recursion.
In this series number of elements of the series is depends upon the input of users. Program will print n number of elements in a series which is given by the user as a input.
Before moving directly on the writing Fibonacci series in python program, first you should know
What is Fibonacci Series?
A Fibonacci series is a series in which next number is a sum of previous two numbers.
For example : 0, 1, 1, 2, 3, 5, 8 ……
In Fibonacci Series, first number starts with 0 and second is with 1 and then its grow like,
0
1
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5 and
so on…
What is Recursion?
Recursion is a programming technique where a function calls itself directly or indirectly in order to solve a problem. It breaks down a large problem into smaller instances of the same problem, each of which is easier to manage and solve. Recursion is typically used in cases where a problem can naturally be divided into similar subproblems.
Characteristics of Recursion
- Base Case: This is the condition under which the recursive function will stop calling itself. Without a base case, the function would call itself indefinitely, leading to a stack overflow.
- Recursive Case: This part of the function is where it calls itself to solve a smaller part of the problem.
Example of Recursion: Calculating Factorial
The factorial of a number (denoted as 𝑛!n!) is a classic example of a recursive problem. Factorial of a number 𝑛n is the product of all positive integers less than or equal to 𝑛n. Mathematically, it’s defined as: 𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×1
For example, 5!=5×4×3×2×1=120
How our program will behave?
Its Logic is different from Fibonacci series program in c using iterative method.
Here we have a function named fibonacci() which will take a input and then return element. Each time it will call itself to calculate the elements of the series.
Like if someone given 6 as a input then our program should return,
0, 1, 1, 2, 3, 5
Python Program to Print Fibonacci Series Using Recursive Methods
n = int(input("please give a number for fibonacci series : "))
first,second=0,1
def fibonacci(num):
if num == 0:
return 0
elif num == 1:
return 1
else:
return fibonacci(num-1)+fibonacci(num-2)
print("fibonacci series are : ")
for i in range(0,n):
print(fibonacci(i))
Output:
please give a number for fibonacci series : 6
fibonacci series are :
0
1
1
2
3
5
Program Explanation
- User Input: The program begins by asking the user to enter the number 𝑛n, which determines how many terms of the Fibonacci sequence will be printed.
- Initialization: Two variables,
first
andsecond
, are initialized to 0 and 1. These are the first two terms of the Fibonacci sequence, although they are not directly used in the recursive function. - Recursive Function: A function named
fibonacci
is defined to compute Fibonacci numbers:- It checks if the input
num
is 0, returning 0 (base case). - It checks if
num
is 1, returning 1 (second base case). - For all other values, it returns the sum of the Fibonacci number at positions
num-1
andnum-2
, effectively calling itself twice for each call (recursive case).
- It checks if the input
- Print Sequence: The program prints a header “fibonacci series are : ” and then iterates from 0 to 𝑛−1 , calling the
fibonacci
function for each value and printing the result. This prints each term of the Fibonacci sequence from the first to the nth term.