Fibonacci Series in Python

Fibonacci series in Python | In the Fibonacci series, the next element will be the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on…

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

As a rule, the expression is Xn= Xn-1+ Xn-2

Fibonacci Sequence in Python

This python program using the if-else statement and while loop to display the Fibonacci sequence. We will take the n-th term while declaring the variables. Python program to display the Fibonacci sequence using while loop and finally, the result will be displayed on the screen.

# Python program to print fibonacci series up to n-th term

# take input
num = int(input('Enter number of terms: '))

# print fibonacci series
a, b = 0, 1
i = 0
    
# check if the number of terms is valid
if num <= 0:
    print('Please enter a positive integer.')

elif num == 1:
    print('The Fibonacci series: ')
    print(a)

else:
    print('The Fibonacci series: ')
    while i < num:
        print(a, end=' ')
        c = a + b
        a = b
        b = c
        i = i+1

Output for the different input values:-

Enter number of terms: 5
The Fibonacci series:
0 1 1 2 3

Enter number of terms: 13
The Fibonacci series:
0 1 1 2 3 5 8 13 21 34 55 89 144

Fibonacci Series in Python using For loop

In the previous program, we will use the while loop but in this program, display the Fibonacci sequence using for loop.

# Python program to print fibonacci series up to n-th term

# take input
num = int(input('Enter number of terms: '))

# print fibonacci series
a, b = 0, 1
   
# check if the number of terms is valid
if num <= 0:
    print('Please enter a positive integer.')

elif num == 1:
    print('The Fibonacci series: ')
    print(a)

else:
    print('The Fibonacci series: ')
    for i in range (1, num+1):
        print(a, end=' ')
        c = a + b
        a = b
        b = c

Output:-

Enter number of terms: 10
The Fibonacci series:
0 1 1 2 3 5 8 13 21 34

Find nth Fibonacci Number

# Python program to find n-th fibonacci number

# take input
num = int(input('Enter number of terms: '))

# print fibonacci term
a, b = 0, 1
    
# check if the number of terms is valid
if num <= 0:
    print('Please enter a positive integer.')

elif num == 1:
    print('The Fibonacci term is = ', end='')
    print(a)

else:
    print('The Fibonacci term is = ', end='')
    for i in range (2, num):
        c = a + b
        a = b
        b = c
    print(b)

Output for the different input values:-

Enter number of terms: 5
The Fibonacci term is = 3

Enter number of terms: 0
Please enter a positive integer.

Enter number of terms: 1
The Fibonacci term is = 0

Enter number of terms: 25
The Fibonacci term is = 46368

We can also find the Fibonacci series using the recursion technique. For more see:- Fibonacci series using recursion

Also See:- Find Factorial of a Number in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *