Python Program To Display Fibonacci Series Up To N Terms

404

In this Python program, you will learn how to display Fibonacci sequence or series up to N terms. Before moving ahead, you should be aware of the following basic concepts of the Python programming language:

  • Variables and operators
  • For loop
  • While loop
  • If…else statement

Must check resources for Python:

What is Fibonacci Series?

Fibonacci series is a sequence of numbers where a number is found by adding the previous two numbers. For example, consider this series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, and 21.

If you notice the pattern in this series, then you will notice that it satisfies this equation:

Fn = Fn-1 + Fn-2

Hence, using this equation, you can find the Fibonacci series up to N terms.

To learn more about Fibonacci Series, check out its Wikipedia page.

In this tutorial, you will learn how to write a Python program to display the Fibonacci series up to N terms using three methods:

  • While loop
  • For loop
  • Recursion

Let’s now see each of these methods.

Method 1: Printing Fibonacci Series in Python Using While Loop

Here we will take input from users for how many terms they would like to see in the Fibonacci series. After that, we will use the if…else Python statements to validate user input and display the results if the number equals 1.

In the main code of our Python program, we will use the while loop to display the Fibonacci series, and the counter variable will ensure it breaks the loop when it becomes greater than the total terms defined by the user.

Here’s the code. Please feel free to copy-paste it and see how it works yourself.

#Python program to display fibonacci series up to n terms

#First two terms
firstterm = 0
secondterm = 1
counter = 0

#Total terms to print the fibonaaci sequence
totalterms = int(input("Please enter how many terms: "))

#Validating user input
if totalterms <= 0:
   print("Please enter only positive integers")
#If there is only one term as input   
elif totalterms==1:
   print(firstterm)
else:
   print("Fibonacci Series: ")
   while counter<totalterms:
      print(firstterm)
      #Updating values for next iteration
      sum = firstterm+secondterm
      firstterm = secondterm
      secondterm = sum
      counter=counter+1

Output:

Please enter how many terms: 10
Fibonacci Series:
0
1
1
2
3
5
8
13
21
34

Method 2: Printing Fibonacci Series in Python Using For Loop

This Python program mainly uses the same logic as in Method 1. The only difference is that here will use the for loop to display the Fibonacci series instead of the while loop.

#Python program to display fibonacci series up to n terms

#First two terms
firstterm = 0
secondterm = 1

#Total terms to print the fibonaaci sequence
totalterms = int(input("Please enter how many terms: "))

#Validating user input
if totalterms <= 0:
   print("Please enter only positive integers")
#If there is only one term as input   
elif totalterms==1:
   print(firstterm)
else:
   print("Fibonacci Series: ")
   print(firstterm)
   print(secondterm)
   for i in range(2, totalterms):
      sum = firstterm+secondterm
      firstterm = secondterm
      secondterm = sum
      print(sum)

Output:

Please enter how many terms: 5
Fibonacci Series:
0
1
1
2
3

Method 3: Printing Fibonacci Series in Python Using Recursion

Now, let’s see how to print the Fibonacci series in Python using recursion.

  • Fibonacci(num) is the main function of calculating the Fibonacci series.
  • The above function will return 0 if the input number by the user is equal to 0. Similarly, if it is equal to 1, it will return 1 and break the loop.
  • The recursive Fibonacci (n) function will calculate the next sequence of the series.
  • For loop will call the above function recursively and display the Fibonacci series.

Here’s the code.

def Fibonacci(num):
   if(num == 0):
      return 0
   elif(num == 1):
      return 1
   else:
      return (Fibonacci(num - 2)+ Fibonacci(num - 1))

num = int(input("Please enter how many terms: "))
for n in range(0, num):
           print(Fibonacci(n))

Output:

Please enter how many terms: 7
0
1
1
2
3
5
8

Wrapping Up

Other Python Program Examples

Advanced Python Tutorials