Rewrite the fib method in listing 182 using iterations hint


(Fibonacci numbers) Rewrite the fib method in Listing 18.2 using iterations. Hint: 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:

f0 = 0; // For fib(0) f1 = 1; // For fib(1)
for (int i = 1; i <= n;=""> { currentFib = f0 + f1;
f0 = f1;
f1 = currentFib;
}
// After the loop, currentFib is fib(n)

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

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Rewrite the fib method in listing 182 using iterations hint
Reference No:- TGS01364580

Expected delivery within 24 Hours