Fibonacci Series: A sequence of numbers in which each number is the sum of the previous two numbers.
First 10 Fibonacci numbers: 0,1,1,2,3,5,8,13,21,34,…
In the following python program we have used recursive function to find out the Fibonacci series up to a given term.
The logic of the program is quite simple.
we have used two base cases to terminate the recursion.
def GenerateFibonaci(x):
if(x == 0):
return 0
elif(x == 1):
return 1
else:
return GenerateFibonaci(x-1) + GenerateFibonaci(x-2)
#main
x = int(input("Enter the term till which you wnat to generate fibonacci sequence: "))
for i in range(x):
print(GenerateFibonaci(i))
Output:
Enter the term till which you wnat to generate fibonacci sequence: 10
0
1
1
2
3
5
8
13
21
34
Note: Execute this program with python3
You can take this Python Quiz