Fibonacci Series in Python

Find the Fibonacci Series up to Nth Term in Python

Given an integer as an input, the objective is to find the Fibonacci series until the number input as the Nth term. Therefore, we write a program to Find the Fibonacci Series up to Nth Term in Python Language.

Example
Input : 4
Output : 0 1 1 2

Lets have a look at write a program to print fibonacci series in python

Find the Fibonacci Series up to Nth Term in Python

Find the Fibonacci Series up to Nth Term in Python Language

Given an integer input as the Nth value, the objective is to Find the Fibonacci Series up to the Nth Term using Loops and Recursion. The objective is to print all the number of the Fibonacci series until the Nth term given as an input. Here are some of the methods to solve the above mentioned problem

  • Method 1: Using Simple Iteration
  • Method 2: Using Recursive Function
  • Method 3: Using direct formulae

We’ll discuss the above mentioned methods in detail in the sections below. Do check out the blue box below for better understanding of fibonacci series program in python.

Fibonacci Series in Python

Method 1: Using Simple Iteration

In this method we’ll use loops to iterate through and form the series up to the integer input N as the range. To print the series up to the Nth term we start a loop from 2 to the Nth term as 0 and 1 are the seed values for forming the series. 

Let’s implement the logic in Python Language.

Run

# Write a program to print fibonacci series upto n terms in python
num = 10
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
    n3 = n1 + n2
    n1 = n2
    n2 = n3
    print(n3, end=" ")

print()

Python Code

Output

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 

Method 2: Using Recursive Function

In this method we’ll use recursion to find the Fibonacci Series up to the given integer input as the Nth range. To do so we take three variables and call the recursive function twice in return statement forming a recursion tree. To know more about recursion, Check out our page, Recursion in Python.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
# Python program to print Fibonacci Series
def fibonacciSeries(i):
if i <= 1:
return i
else:
return (fibonacciSeries(i - 1) + fibonacciSeries(i - 2))

num=10
if num <=0:
print("Please enter a Positive Number")
else:
print("Fibonacci Series:", end=" ")
for i in range(num):
print(fibonacciSeries(i), end=" ")

Output

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

Method 3

We can use direct formula to find nth term of Fibonacci Series as –

Fn = {[(√5 + 1)/2] ^ n} / √5

Run
# write a program to print fibonacci series in python
import math

def fibonacciSeries(phi, n):
    for i in range(0, n + 1):
        result = round(pow(phi, i) / math.sqrt(5))
        print(result, end=" ")


num = 10
phi = (1 + math.sqrt(5)) / 2
fibonacciSeries(phi, num)

Output

Fibonacci Series:0 1 1 2 3 5 8 13 21 34 55

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

20 comments on “Fibonacci Series in Python”


  • Deepsaran

    num = int(input(“Enter the length of series”)
    a, b = 0, 1
    for i in range(num):
    print(a)
    a, b = b, a+b


  • Tikeswar

    def fibonacciSeries(i):
    if i <= 1:
    return i
    else:
    return (fibonacciSeries(i – 1) + fibonacciSeries(i – 2))

    num=int(input())
    if num <= 0:
    print("Please enter a Positive Number")
    else:
    print("Fibonacci Series:", end=" ")
    for i in range(0,num):
    print(fibonacciSeries(i), end=" ")


  • Ayushi

    nterms = int(input(“Enter a number: “))
    n1 = 0
    n2 = 1
    count = 0

    while count < nterms:
    print(n1, end=" ")
    nth = n1 + n2
    n1 = n2
    n2 = nth
    count += 1


  • Ahamed

    n1 = int(input())
    s = [0,1]
    for i in range(2, (n1)):
    j = s[i-2] + s[i-1]
    s.append(j)
    print(s)


  • sandesh

    a=[0,1]
    n=int(input(‘enter no term required ‘))
    for i in range(1,n-1):
    a.append(a[i-1]+a[i])
    for j in range:
    print(j,end=’,’)


  • Md

    def f(n):
    if n==0:
    return 0
    elif n==1:
    return 1
    else:
    return f(n-1)+f(n-2)
    for n in range(10)
    print(f(n))


  • ROHAN

    n = int(input())

    a = 0
    b = 1
    i = 0
    c = 0

    print(a)
    print(b)
    while i < n-2:
    c = a+b
    print(c)
    a=b
    b=c

    i+=1


  • gaurav

    n = int(input(“enter number(>2): “))
    k = [0,1]

    for i in range(2,n):
    j = k[i-1] + k[i-2]
    k.append(j)
    print(k)


  • Pavithra

    d=[0,1]
    k=int(input())
    for j in range(k):
    if (len(d) == k):
    break
    else:
    u=d[j]+d[j+1]
    d.append(u)
    for i in d:
    print(i,end=”,”)


  • Ega

    def fibonacciSeries(i):
    if i <= 1:
    return i
    else:
    return (fibonacciSeries(i – 1) + fibonacciSeries(i – 2))

    num=int(input('enter number')
    if num <= 0:
    print("Please enter a Positive Number")
    else:
    print("Fibonacci Series:", end=" ")
    for i in range(num):
    print(fibonacciSeries(i), end=" ")

    **he forgot to give num input in the fist method


  • Sunil

    n=int(input(“Enter how many number you want in the series: “))
    First=0
    Second=1
    for i in range(n):
    print(First)
    temp=First
    First=Second
    Second=temp+Second