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)
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:
Write a test program that prompts the user to enter an index and displays its Fibonacci number.
Listing


f0 = 0; // For fib(0) f1 = 1; // For fib(1) for (int i = 1; i
Step by Step Solution
3.46 Rating (169 Votes )
There are 3 Steps involved in it
Output Enter an index for the fibonacci number 34 The Fibonacci is 9... View full answer
Get step-by-step solutions from verified subject matter experts
