FileDocCategorySizeDatePackage
Fibonacci.javaAPI DocExample1144Mon Sep 22 13:30:30 BST 1997None

Fibonacci

public class Fibonacci extends Object
This program prints out the first 20 numbers in the Fibonacci sequence. Each number is formed by adding together the previous two numbers in the sequence, starting with the numbers 0 and 1.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    int current, prev = 1, prevprev = 0;// Initialize some variables
    for(int i = 0; i < 20; i++) {       // Loop exactly 20 times
      current = prev + prevprev;        // Next number is sum of previous two
      System.out.print(current + " ");  // Print it out
      prevprev = prev;                  // First previous becomes 2nd previous
      prev = current;                   // And current number becomes previous
    }
    System.out.println();               // Terminate the line, and flush output