by BehindJava

# Write a program to Display Fibonacci Sequence Using Recursion in python

Home » python » Write a program to Display Fibonacci Sequence Using Recursion in python

In this tutorial we are going to learn about writing a program to Display Fibonacci Sequence Using Recursion.

# Python Program to Display Fibonacci Sequence Using Recursion

``````# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n &lt;= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = int(input(“enter no of terms”))

# check if the number of terms is valid
if nterms &lt;= 0:
print(&quot;Plese enter a positive integer&quot;)
else:
print(&quot;Fibonacci sequence:&quot;)
for i in range(nterms):
print(recur_fibo(i))``````

Explanation:

• In Fibonacci series ,the sequence is like n-1+n-2=n
• 0,1,1,2,3,5,8,13,21….
• so ,here first in recur_ fibbo function,we are using if and else statement.if the number is less than or equal to one which is starting of sequence it will directly assign n to it else if n is greater than 1 it uses return statement to add n-1 and n-2.
• Now, take iput number from user.
• Use another if and else statement to check the validity of no of terms,enter a number less than 0 then it asks you to enter positive number else it will use for loop and iterate over nterms .
• Finally,we call the function with iterator and print fibanocci series.