Rewrite the fib method in Listing 18.2 using iterations. To compute fib(n) without recursion, you need to

Question:

Rewrite the fib method in Listing 18.2 using iterations. To compute fib(n) without recursion, you need to obtain fib(n - 2) and fib(n - 1) first. Let f0 and f1 denote the two previous Fibonacci numbers. The current Fibonacci number would then be f0 + f1. The algorithm can be described as follows:image

Write a test program that prompts the user to enter an index and displays its Fibonacci number.

Listing

image

image

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: