A Program to Display the Fibonacci Series
In this example, we are giving a program to display the fibonacci series. Fibonacci is a series where a term is the sum of previous two terms. The 1st two terms of the Fibonacci series is 0 followed by 1.
Program to display the fibonacci series:
# Display the fibonacci series
nterms = int(input('Enter the term: '))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output:
Enter the term: 8 Fibonacci sequence: 0 1 1 2 3 5 8 13