Python program for Fibonacci series using Recursion
What is the Fibonacci series
The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
- The 2 is found by adding the two numbers before it (1+1)
- The 3 is found by adding the two numbers before it (1+2),
- And the 5 is (2+3),
- and so on!
terms = int(input("how many terms? : ")) #taking the input from the user
#using recursion to print the series
def recur_fibo(n,a1,a2):
if n <= 1:
#return this if terms is less than 1
print(a1)
return
else:
print(str(a1) , end=" , ")
return(recur_fibo(n-1,a2,a1 + a2))
#checking for the negative number and single terms
if terms <= 0:
print("Please enter a positive integer")
elif terms == 1:
print("Fibonacci Sequence of 1 terms : ")
print(0)
else:
recur_fibo(terms,0,1)
to verify the indentation of the program screenshot of the program is
Output for this program is
output |
Thanks for being here.
Any problem or suggestion please comment below.
Comments
Post a Comment