public class Fibonacci {
public static long fib(int n) {
if (n <= 1) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
public static void main(String[] args) {
for (int x = 0; x <= 15; x++) {
long total = 0;
for (int i = 1; i <= x; i++) {
total = fib(i);
}
System.out.print(total + ", ");
}
System.out.println("...");
}
}
Output:
$ java Fibonacci 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, ...