Python Program to Find n-th term of Fibonacci Series

Python program to n-th term of a fibonacci series; Through this tutorial, i am going to show you how to find n-th term of a fibonacci series in python.

In this tutorial, i will write python programs to find the nth term in the fibonacci series using for loop, while loop and recursion. And as well as, write a python program to find the sum of fibonacci series.

Python Program to Find n-th term of Fibonacci Series

Let’s see the following python programs to find n-th term of a fibonacci series; as shown below:

  • Fibonacci series in python using for loop
  • Fibonacci series python programming using while loop
  • Fibonacci series in python using recursion
  • Sum of fibonacci series in python

Fibonacci series in python using for loop

# Take input from user              
a=int(input("Enter the terms"))
f=0  
s=1
if a<=0:
    print("The requested series is ",f)
else:
    print(f,s,end=" ")
    for x in range(2,a):
        next=f+s                           
        print(next,end=" ")
        f=s
        s=next

Output

Enter the terms 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Fibonacci series python programming using while loop

# Python Fibonacci series Program using While Loop
# Fibonacci series will start at 0 and travel upto below number
Number = int(input("\nPlease Enter the Range Number: "))
# Initializing First and Second Values of a Series
i = 0
First_Value = 0
Second_Value = 1
           
# Find & Displaying Fibonacci series
while(i < Number):
           if(i <= 1):
                      Next = i
           else:
                      Next = First_Value + Second_Value
                      First_Value = Second_Value
                      Second_Value = Next
           print(Next)
           i = i + 1

Output

Please Enter the Range Number:  10
0
1
1
2
3
5
8
13
21
34

Fibonacci series in python using recursion

def FibRecursion(n):  
   if n <= 1:  
       return n  
   else:  
       return(FibRecursion(n-1) + FibRecursion(n-2))  
nterms = int(input("Enter the terms? "))  # take input from the user
  
if nterms <= 0:  # check if the number is valid 
   print("Please enter a positive integer")  
else:  
   print("Fibonacci sequence:")  
   for i in range(nterms):  
       print(FibRecursion(i))

Output

Enter the terms?  15
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Sum of fibonacci series in python

# Python to calculate sum of Fibonacci numbers
  
  
# Computes value of first 
# fibonacci numbers 
def calSum(n) : 
    if (n <= 0) : 
        return 0
   
    fibo =[0] * (n+1) 
    fibo[1] = 1
   
    # Initialize result 
    sm = fibo[0] + fibo[1] 
   
    # Add remaining terms 
    for i in range(2,n+1) : 
        fibo[i] = fibo[i-1] + fibo[i-2] 
        sm = sm + fibo[i] 
          
    return sm 
  
#take input from user
n=int(input("Enter the terms"))
#call calSum() function and print result
print("Sum of Fibonacci numbers is : " , 
      calSum(n)) 

Output

Enter the terms 10 
Sum of Fibonacci numbers is :  143 

Recommended Python Tutorials

Leave a Comment