Python Recursion Program

# Python program to display the Fibonacci series using recursion
def fibonacci(n):
   if n <= 1:
       return n
   else:
       return(fibonacci(n-1) + fibonacci(n-2))

nterms = int(input("enter the no. of terms"))

# check if the number of terms is valid
if nterms<= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci series:")
   for i in range(nterms):
       print(fibonacci(i))

Output

enter the no. of terms7
Fibonacci series:
0
1
1
2
3
5
8


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext