In this post, we will see how we can print fibonacci series of numbers in python
Logic
First two numbers are 0 and 1, and then next subsequent integer will be sum of the previous 2 numbers
Implementation
We will see 2 ways of implementation, using
- Recursive method
- for loop
Method1
def fibo(i):
if i <= 1:
return i
else:
return (fibo(i - 1) + fibo(i - 2))
num = 10
for i in range(num):
print(fibo(i))
Method2
num = 10
n1 = 0
n2 = 1
for i in range(num):
print(n1)
temp = n1 + n2
n1 = n2
n2 = temp
Output
0
1
1
2
3
5
8
13
21
34


