Fibonacci Series Program in Python
In this post, you will learn how to write a Fibonacci series program using the Python programming language.
The fibonacci series is the sequence of numbers in which the next number is the sum of the previous two numbers. The Fibonacci series was known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember the Fibonacci Sequence by using November 23rd as the Fibonacci Day. November 23rd has the digits "1, 1, 2, 3", which is part of the sequence.
In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule, the third number is 1 (sum of 0 and 1). The fourth number is 2 and so on.
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....
Fibonacci series program in Python using while loop
In the given Python program, you will learn how to print the Fibonacci series using the while loop. In this code, x1 contains the first number, i.e., 0 and x2 contains the second number, i.e., 1 and xth contains the total Fibonacci series number count. In each iteration of the while loop, we get the next number by adding the previous two numbers, and simultaneously we swap the first number with the second and the second with the third.
series = int(input("How many number of series? "))
x1, x2 = 0, 1
count = 0
if series <= 0:
("Invalid: Please enter a positive integer")
elif series == 1:
print("Fibonacci series upto ",series,":")
print(x1)
else:
print("Fibonacci series sequence:")
while count < series:
print(x1)
xth = x1 + x2
x1 = x2
x2 = xth
count += 1
Output of the above code:
How many number of series? 30
Fibonacci series sequence:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
Python fibonacci series program using recursion function
A recursion function is a function that is called by itself. In the given Python program, we call the recursion function to get the Fibonacci series. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
def fibonacci_recur(n):
if n <= 1:
return n
else:
return(fibonacci_recur(n-1) + fibonacci_recur(n-2))
# take input from the user
series = int(input("How many number of series? "))
# check the valid input
if series <= 0:
print("Invalid: Plese enter a positive integer")
else:
print("Fibonacci of ",series," is :")
for i in range(series):
print(fibonacci_recur(i))
Output of the above code:
How many number of series? 15
Fibonacci of 15 is :
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
Python fibonacci series program using for loop
In the given Python program, we have used the Fibonacci series program using the for loop.
# Fibonacci series in python using for loop
n=int(input("How many number of series? "))
a=0
b=1
if n<=0:
print("The output of your input is",a)
else:
print("Fibonacci series sequence:\n")
print(a,b,end="\n")
for x in range(2,n):
c=a+b
print(c,end="\n")
a=b
b=c
Output of the above code:
How many number of series?
15
Fibonacci series sequence:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
Related Articles
Permutation and Combination in PythonCube root in Python
Python program to find area of circle
Python program to multiply two numbers
How to read xml file in Python
Python Spell Checker Program
Python remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert xml to dict
Python convert dict to xml
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers