Articles

Program to print Fibonacci series in python up to a give number

Program to print Fibonacci series in python up to a give number


In this python programming article, we are going to learn

  • program to print fibonacci series in python

Program to print first n fibonacci numbers in python

The python program to print fibonacci series using while loop is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

number = int(input("Enter upper limit: "))

a = 0
b = 1
sum_fib = 0
print(a,b, end=" ")
while True:
    sum_fib = a + b
    a = b
    b = sum_fib
    if sum_fib < number:
        print(sum_fib, end=" ")
    else:
        break

The output of the python program to print fibonacci numbers is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py                         En
Enter upper limit: 20
0 1 1 2 3 5 8 13
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter upper limit: 24
0 1 1 2 3 5 8 13 21

Few important tips about the program

1. The fibonacci series is such a series which begins with 0 and 1 and the rest of the terms are the sums of their previous two terms.

2. Here, the user has given an upper limit which we have converted to int data type.

3. Thereafter, we found the sum of two numbers and we have assigned the second number to first number, the sum of the two numbers to the second number.

4. We printed the sum out, if it is lesser than the upper limit.

 

Python program to print fibonacci series using while loop snapshot:

python program to print fibonacci series using while loop

Using above concept, you can try to write program to print fibonacci series in python using for loop. Give it a try.


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Sep 04,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!