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:

  1. # Display the fibonacci series
  2. nterms = int(input('Enter the term: '))
  3. n1, n2 = 0, 1
  4. count = 0
  5. if nterms <= 0:
  6. print("Please enter a positive integer")
  7. elif nterms == 1:
  8. print("Fibonacci sequence upto",nterms,":")
  9. print(n1)
  10. else:
  11. print("Fibonacci sequence:")
  12. while count < nterms:
  13. print(n1)
  14. nth = n1 + n2
  15. n1 = n2
  16. n2 = nth
  17. count += 1

Output:

Enter the term: 8
Fibonacci sequence:
0
1
1
2
3
5
8
13