Recursive Fibonacci


 Recursive Fibonacci

A Fibonacci sequence is a sequence of integers, which first two terms are 0 and 1 and all other terms of sequence are obtained by adding their preceding two values.

Example : 0,1,1,2,3,5,8,13,21,34.......... and so on.

Program to print Fibonacci sequence up to a given limit.

def recur(n):

        if(n<=1):

                return n

        else:

                return(recur(n-1)+recur(n-2))

# take input from the user.

nterms=int(input("How many terms"))

# checks if the number of terms is invalid.

if (nterms<=0):

        print("Please enter a positive number")

else:

        print("Fibonacci sequence:")

        for i in range(nterms):

                print(recur(i))





No comments:

Post a Comment