Skip to main content

Write a recursive code to compute the nth Fibonacci number.


#Program using a recursive function to print fibonacci series upto nth term.
#Code
def fib(n):
        if n==1:                                    #1st term is 0
                return 0
        elif n==2:                                  #2nd term is 1
                return 1
        else:
                return fib(n-1)+fib(n-2)

#_main_
n=int(input("Enter last term required:"))
for i in range(1,n+1):                         #list with value 1..n
        print(fib(i),end=',')
print("...")

OUTPUT

Comments

Popular posts from this blog