Program to print the Fibonacci sequence in Python

Program to print the Fibonacci sequence in Python. This programs contain if else conditions, input of number, While loop etc. This program is useful for beginners, class 11,12 and B Tech computer science students.

Program to print the Fibonacci sequence in Python

Fibonacci sequence:

Fibonacci sequence is a series of numbers where the next number is found by adding up the two numbers just before it.

Python Program to Print the Fibonacci sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … and so on

In mathematical terms, the sequence “Fn” of the Fibonacci sequence of numbers is defined by the recurrence relation:
Fn= Fn_1+ Fn_2
Where seed values are:
F0=0 and F1=1

Source code :

n_terms = int(input ("How many terms the user wants to print? "))  
  
# First two terms  
n_1 = 0  
n_2 = 1  
count = 0  
  
# Now, we will check if the number of terms is valid or not  
if n_terms <= 0:  
    print ("Please enter a positive integer, the given number is not valid")  
# if there is only one term, it will return n_1  
elif n_terms == 1:  
    print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")  
    print(n_1)  
# Then we will generate Fibonacci sequence of number  
else:  
    print ("The fibonacci sequence of the numbers is:")  
    while count < n_terms:  
        print(n_1)  
        nth = n_1 + n_2  
       # At last, we will update values  
        n_1 = n_2  
        n_2 = nth  
        count += 1  

Download Source code : For Download Click on the below Button

Step wise Source code Explanation :

Step 1: Input the number of values we want to generate the Fibonacci sequence
Step 2: Initialize the count = 0, n_1 = 0 and n_2 = 1.
Step 3: If the n_terms <= 0
Step 4: print “error” as it is not a valid number for series
Step 5: if n_terms = 1, it will print n_1 value.
Step 6: while count < n_terms
Step 7: print (n_1)
Step 8: nth = n_1 + n_2
Step9: we will update the variable, n_1 = n_2, n_2 = nth and so on, up to the required term.

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
How many terms the user wants to print? 10
The fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
13
21
34
>>> 

Output Explanation:

How many terms the user meant to print is captured as input.
The program start printing the series for 10 numbers.

Conclusion:

The program runs fine.

Thank you for reading and learning from Program to print the Fibonacci sequence in Python post. This programs contain if else conditions, input of number, While loop etc. This program is useful for beginners, class 11,12 and B Tech computer science students. For more programs and practical file of 11 and 12 click below.

Python Programs Archives – CS Study

Computer Science Practical file for class 12 Term-2 – CS Study

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Leave a Comment

Your email address will not be published.