Fibonacci series program in Python
Written by
Python program for Fibonacci series(Using for loop)
Fibonacci series is the program based on Fibonacci spiral. This series is formed by addition of 2 previous numbers to coin the third term. Initiated by two numbers 0 and 1.
In this program we are going to generate a Fibonacci series using python.
For example:
Series contain
First term =0
Second term=1
Third term= 0+1=1
Fourth term= 1+1=2
.
.
So on.
Algorithm:
- Initialise the first 2 numbers as 0 and 1.
- Define a for loop for the number of terms you want in the series.
- Print the function.
- Exit.
Code:
#Program to print Fibonacci series
n= int(input("enter the number of elements you want in series:"))
c=[]
c.append(0)
c.append(1)
a=0
b=1
d=0
for i in range( 1, n-1):
d=a+b
c.append(d)
a=b
b=d
for i in c:
print(i)
Output:
enter the number of elements you want in series: 4
0
1
1
2