01: import java.util.Scanner;
02: 
03: /**
04:    This program computes Fibonacci numbers using an iterative method.
05: */ 
06: public class FibLoop
07: {  
08:    public static void main(String[] args)
09:    {  
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: 
21:    /**
22:       Computes a Fibonacci number.
23:       @param n an integer
24:       @return the nth Fibonacci number
25:    */
26:    public static long fib(int n)
27:    {  
28:       if (n <= 2) return 1;
29:       long fold = 1;
30:       long fold2 = 1;
31:       long fnew = 1;
32:       for (int i = 3; i <= n; i++)
33:       {  
34:          fnew = fold + fold2;
35:          fold2 = fold;
36:          fold = fnew;
37:       }
38:       return fnew;
39:    }
40: }