In this article, we have provided a python source code which can display a Fibonacci Sequence using the recursive technique.
Prerequisite Python Program to Display Fibonacci Sequence Using Recursion
- Python Input, Output
- Python Functions
- Python Recursion
Fibonacci Sequence: A Fibonacci sequence is an integer series which start from 0 and 1 and each next integer is the sum of its previous two integers. for instance 0, 1 , 1, 2, 3, 5, 8, 13, ..... is a Fibonacci series. Recursion: Recursion is a technique in which the function calls itself again and again till the base condition get satisfied.
Steps:
- Ask the user to enter a number, which represents the number of integers to display from the Fibonacci series.
- Create a recursive function which acts as a loop and call the function again and again till we get the range entered by the user.
Python Program to Display Fibonacci Sequence Using Recursion
Python Code
def fibo(n,first =0,second =1):
if n<1:
print("Done with Recursion")
else:
f=first
print(f)
s=second
now= f+s
f= second
s= now
#recursion
fibo(n-1,f,s)
num = int(input("Enter how many terms you want to display from Fibonacci Sequence: "))
fibo(num)
Output 1:
Enter how many terms you want to display from Fibonacci Sequence: 15
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
Done with Recursion
Output 2:
Enter how many terms you want to display from Fibonacci Sequence: 10
0
1
1
2
3
5
8
13
21
34
Done with Recursion
People are also reading:
- Python Program to find Hash of File
- Python Program to Merge Mails
- How many Centimeters in a Foot through Python?
- WAP to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2
- WAP Using Switch Case to Check whether the given alphabet is a vowel or Not
- Linear Search in Python and C++
- WAP in C++ & Python for the Union of Two Arrays
- WAP to find the largest number amongst the numbers entered by the user
- Binary Search in C
- WAP in C++ & Python to Calculate Compound Interest
Leave a Comment on this Post