This post contains a total of 10+ Python Program Examples with Source Code to Print Fibonacci Series. All these Fibonacci Series programs are made using Python.
You can use the source code of these examples with credits to the original owner.
Related Posts
Python Programs to Print Fibonacci Series
1. By Nick
Made by Nick. Fibonacci series program using yield. Source
Fibonacci series of 10 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
#printing fibonacci series
def fib(x):
a,b=0,1
for _ in range(x):
yield a
a,b=b,a+b
x = int(input()) #enter no. of elements to print
print ("Fibonacci series of",x,"terms:")
print(list(fib(x)))
2. By Pranjal Tambe
Made by Pranjal Tambe. Program that prints the Fibonacci series of the amount of number you input, like if you enter 5 then the program will print out the first 5 Fibonacci numbers. Source
0 1 1 2 3
num = int(input())
fib=list(range(num))
def fibonacci(n):
if n<=2:
return fib[n-1]
else:
fib[n-1]=fibonacci(n-1)+fibonacci(n-2)
return fib[n-1]
(fibonacci (num))
for i in fib:
print(i)
3. By Wilson Bol
Made by Wilson Bol. Program to print Fibonacci series numbers upto n. Source
0 1 1 2 3 5 8 13 21 34 55 89
def fib(n):
'''Print a Fibonacci series up to n.'''
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
# Call the function
fib(100)
4. By Harshita Sharma
Made by Harshita Sharma. Very small Python Program for Printing Fibonacci series. Source
0 1 1 2 3 5 8 13 21 34 55 89
a,b=0,1
while b<150:
print(a)
a,b=b,a+b
5. By Ziyua
Made by Ziyua. Python program that prints Fibonacci series from 0 to Infinity. Source
1 1 2 3 5 8 13 ...
i=1
j=1
k=1
while i > 0 :
print(i)
k=i
i=j
j=i+k
6. By Man Huynh
Made by Man Huynh. Program to find nth Number in Fibonacci Series. Source
Tested 40 times with random value upto 20. All 3 functions gave same results. No.1 in Fibonacci series: fib1: 1 fib2: 1 fib3: 1 =============== No.2 in Fibonacci series: fib1: 1 fib2: 1 fib3: 1 =============== No.3 in Fibonacci series: fib1: 2 fib2: 2 fib3: 2 =============== No.4 in Fibonacci series: fib1: 3 fib2: 3 fib3: 3 =============== No.5 in Fibonacci series: fib1: 5 fib2: 5 fib3: 5 =============== No.6 in Fibonacci series: fib1: 8 fib2: 8 fib3: 8 ===============
# Different Functions to show the nth single number in the Fibonacci series
# n: start from 1 (1st)
# Memoization using List
def fib1(n):
fib_list = [1,1]
for i in range(2,n):
fib_list.append(fib_list[i-2]+fib_list[i-1])
return fib_list[n-1]
# Calculation
phi = (5**0.5+1)/2
def fib2(n):
return int((phi**n+(phi-1)**n)/5**0.5)
# Recursion
def fib3(x):
if x == 1 or x == 2:
return 1
else:
return fib3(x-1) + fib3(x-2)
# Testing only
import random
test_times = 40
test_size = 20
for i in range(test_times):
r = random.randint(1,test_size)
flag = (fib1(r) != fib2(r)) or (fib1(r) != fib3(r)) or (fib2(r) != fib3(r))
if flag:
print("WRONG")
else:
print("Tested {} times with random value upto {}.".format(test_times,test_size))
print("All 3 functions gave same results.\n")
# Sample results
for i in range(1,7):
print("No.{} in Fibonacci series:".format(i))
print("fib1: ",fib1(i))
print("fib2: ",fib2(i))
print("fib3: ",fib3(i))
print("===============\n")
7. By Shorya Bhatnagar
Made by Shorya Bhatnagar. Source
10 0 1 1 2 3 5 8 13 21 34
num=int(input())
def fib(x):
if x==1:
return 0
if x==2:
return 1
else:
return fib(x-1)+fib(x-2)
def countdown(n):
i=1
while i < n:
yield i
i = i+1
for i in countdown(num+1):
print(fib(i))
8. By Aatif Fraz
Made by Aatif Fraz. A Simple Fibonacci Series Printer program. Source
0 1 1 2 3 5 8
a, b = 0, 1
limit = int(input('')) # Enter the point till where you would want the series
while a < limit:
print(a)
a, b = b, a + b
9. By Shivesh Yadav
Made by Shivesh Yadav. Source
Enter a no.,till where u you want fibonacci no.s 15 Fibonacci series : 0 1 1 2 3 5 8 13
d=int(input("Enter a no.,till where u you want fibonacci no.s"))
print (" ")
b=0
a=0
c=1
print ("Fibonacci series :")
print ("0")
print ("1")
while (a<d):
a=b+c
b=c
c=a
if (a>d):
break
print (a)
print (" ","Thanks")
10. By SubbaReddy G
Made by SubbaReddy G. Python program to print Fibonacci Series up to given number. Source
Enter the number below which the fibonacci series is needed 20 the required fib series is 1 2 3 5 8 13
f1=1
f2=2
n=int(input("Enter the number below which the fibonacci series is needed"))
print ("\nthe required fib series is \n")
print (f1)
print (f2)
f3=f1+f2
while f3 <= n :
print (f3)
f1=f2
f2=f3
f3=f1+f2
11. By Santhosh
Made by Santhosh. Source
Enter terms :0 1 1 2 3 5 8
n = int(input("Enter terms :"))
a = 0
b = 1
print(a,b)
for i in range(1,n+1):
c = a + b
print (c)
a,b=b,c