01: import javax.swing.JOptionPane;
02: 
03: /**
04:    This program computes Fibonacci numbers using an iterative
05:    method.
06: */ 
07: public class FibLoop
08: {  
09:    public static void main(String[] args)
10:    {  
11:       String input = JOptionPane.showInputDialog("Enter n: ");
12:       int n = Integer.parseInt(input);
13: 
14:       for (int i = 1; i <= n; i++)
15:       {
16:          double f = fib(i);
17:          System.out.println("fib(" + i + ") = " + f);
18:       }
19:       System.exit(0);
20:    }
21: 
22:    /**
23:       Computes a Fibonacci number.
24:       @param n an integer
25:       @return the nth Fibonacci number
26:    */
27:    public static double fib(int n)
28:    {  
29:       if (n <= 2) return 1;
30:       double fold = 1;
31:       double fold2 = 1;
32:       double fnew = 1;
33:       for (int i = 3; i <= n; i++)
34:       {  
35:          fnew = fold + fold2;
36:          fold2 = fold;
37:          fold = fnew;
38:       }
39:       return fnew;
40:    }
41: }