Learn about Fibonacci Series in Python using Recursion in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Fibonacci Series in Python using Recursion
Recursion is one of the fastest and tricky approaches for dealing with problems. The below code examples explains how to generate the Fibonacci series using Recursion. The basic idea of using recursion to generate Fibonacci series is:
fib(n) = fib(n-1) + fib(n-2)
# Python program to print fibonacci series using recursion # recursive function def fibSeries(num): if num <= 1: return num else: return fibSeries(num-1) + fibSeries(num-2) # user input num = int(input('Enter number of terms: ')) # print fibonacci series if num <= 0: print('Please enter a positive integer') else: print('The fibonacci series:') for i in range(num): print(fibSeries(i), end=' ')
Enter number of terms: 5
The fibonacci series: 0 1 1 2 3
Hope the above code works for you and Refer to the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Leap Year Code in Python using Calendar Module