Java Reference
In-Depth Information
Figure 2
Call Pattern of the Recursive fib Method
605
606
ch13/fib/LoopFib.java
1
import
java.util.Scanner;
2
3 /**
4
This program computes Fibonacci numbers using an iterative method.
5 */
6
public class
LoopFib
7 {
8
public static void
main(String[] args)
9 {
10 Scanner in =
new
Scanner(System.in);
11 System.out.print(
"Enter n: "
);
12
int
n = in.nextInt();
13
14
for
(int i =
1
; i <= n; i++)
15 {
16
long
f = fib(i);
17 System.out.println(
"fib("
+ i +
") = "
+ f);
18 }
19 }
20