Answer to Question #3371 in Python for mukunda

Question #3371
Write a Fibonacci program in python using recursive function and looping method.
1
Expert's answer
2011-08-02T13:58:05-0400
def fibonacci_recursive (n):
if n == 0: return 0
elif n == 1: return 1
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)

def fibonacci_iterative (n):
if n == 0: return 0
elif n == 1: return 1

prelast, last = 0, 1
for i in range(n-1):
& new = prelast + last
& prelast, last = last, new

return new

print(fibonacci_recursive(7))
print(fibonacci_iterative(7))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS