Python Program to print and plot the Fibonacci series

Python Program to print and plot the Fibonacci series

The Fibonacci Sequence is a series of numbers named after the Italian mathematician, known as the Fibonacci. It is simply a series of numbers that start from 0 and 1 and continue with the combination of the previous two numbers. 

In this article, you will learn how to write a Python program using the Fibonacci series using many methods. We discuss two examples here in the first example you will learn how to print Fibonaaci series in Python Programming. 

In the second example discuss how to Plot the Fibonacci series in Python Programming using Matplotlib. We also discuss What is the Fibonacci Series and example of fibonacci series.

The following will be discussed below.

What is the Fibonacci Series?

The Fibonacci series is a series of numbers formed by the addition of two increasing numbers in a series.

Fibonacci Series example: 0,1,1,2,3,5

In the example above, 0 and 1 are the first two concepts of the series. These two words are directly printed. The third term is calculated by adding the first two words. In this 0 and 1. So, we get 0 + 1 = 1. So 1 is printed as the third term. The next word is produced using the second and third terms and does not use the first term. It is done until the number of words you want or requested by the user. In the example above, we have used five terms.


Example 1: To print the Fibonacci series in Python

fib1 = int(input('enter first term'))
fib2 = int(input('enter second term'))
n = int(input('enter the number of terms'))
print(fib1)
print(fib2)
m = 3while(m<=n):
    fib3 = fib1 + fib2
    print(fib3)
    fib1 = fib2
    fib2 = fib3
    m = m+1
Output:
enter first term1
enter second term2
enter the number of terms4
1
2
3
5

Example 2: Program to print and plot Fibonacci Series using Matplotlib


import numpy as np
from matplotlib import pyplot as plt
m =[]
fib1 = int(input('enter first term'))
fib2 = int(input('enter second term'))
n = int(input('enter the number of terms'))
y = np.linspace(1,n,n,dtype=int)
print('fibonicci series is:')
print(fib1)
print(fib2)
m.append(fib1)
m.append(fib2)
i = 3while(i<=n):
    fib3 = fib1 + fib2
    print(fib3)
    fib1 = fib2
    fib2 = fib3
    m.append(fib3)
    i = i + 1print(m)
plt.plot(y,m,'r*')
plt.xlabel('position of terme')
plt.ylabel('value of terms')
plt.title('fibonicci series')
plt.show()
Output:
enter first term1
enter second term3
enter the number of terms7
fibonicci series is:
1
3
4
7
11
18
29
[1, 3, 4, 7, 11, 18, 29]
Python Program to print and plot the Fibonacci series
Python Program to print and plot the Fibonacci series