Recursive fibonacci function. : Function Recursive « Function « Python






Recursive fibonacci function.

Recursive fibonacci function.



def fibonacci( n ):

   if n == 0 or n == 1:  # base case
      return n
   else:

      # two recursive calls
      return fibonacci( n - 1 ) + fibonacci( n - 2 )

number = int( raw_input( "Enter an integer: " ) )

if number > 0:
   result = fibonacci( number )
   print "Fibonacci(%d) = %d" % ( number, result )
else:
   print "Cannot find the fibonacci of a negative number"
 

           
       








Related examples in the same category

1.Recursive definition of function factorialRecursive definition of function factorial
2.Function recursive DemoFunction recursive Demo