How to Find nth term of a Fibonacci Series?

31-Oct-2022

.

Admin

How to Find nth term of a Fibonacci Series?

Hi Dev,

Now, let's see example of How to Find nth term of a Fibonacci Series. if you have question about nth Term of Fibonacci Series Using in Python then I will give simple example with solution. it's simple example of Fibonacci Series in Python. you can see nth Fibonacci number in Python. Follow bellow tutorial step of Fibonacci Sequence in Python Example.

Program to find nth fibonacci number in python; In this tutorial, you will learn how to find nth term in fibonacci series in python using for loop, while loop and recursion function.

let's see below simple example with output:

Example 1: Fibonacci series in python using for loop


# Take input from user

x=int(input("Enter the terms"))

f=0

s=1

if x<=0:

print("The requested series is ",f)

else:

print(f,s,end=" ")

for x in range(2,x):

next=f+s

print(next,end=" ")

f=s

s=next

Output:

Enter the terms 10

0 1 1 2 3 5 8 13 21 34

Example 2: 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: 7

0

1

1

2

3

5

8

Example 3: 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? 10

Fibonacci sequence:

0

1

1

2

3

5

8

13

21

34

Example 4: 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

I hope it can help you...

#Python