python fibonacci sequence recursion code example
Example 1: python recursive fibonacci function
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Example 2: python fibonacci recursive
def Fibonacci( pos ):
#check for the terminating condition
if pos <= 1 :
#Return the value for position 1, here it is 0
return 0
if pos == 2:
#return the value for position 2, here it is 1
return 1
#perform some operation with the arguments
#Calculate the (n-1)th number by calling the function itself
n_1 = Fibonacci( pos-1 )
#calculation the (n-2)th number by calling the function itself again
n_2 = Fibonacci( pos-2 )
#calculate the fibo number
n = n_1 + n_2
#return the fibo number
return n
#Here we asking the function to calculate 5th Fibonacci
nth_fibo = Fibonacci( 5 )
print (nth_fibo)