Java Fibonacci
Fibonacci implementation
In mathematics, the Fibonacci numbers are the numbers in sequence. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
public class Main {
public static void main(String[] args) {
int n0 = 1, n1 = 1, n2;
System.out.print(n0 + " " + n1 + " ");
/* w ww . j a v a 2 s .c o m*/
for (int i = 0; i < 18; i++) {
n2 = n1 + n0;
System.out.print(n2 + " ");
n0 = n1; // First previous becomes 2nd previous
n1 = n2; // And current number becomes previous
}
System.out.println(); // Terminate the line
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: