2347 views.
/*
* Creating a program to print fibonacci series.
*/
class FibonacciSeries {
/*
* To perform fibonacci Series
* fib(n) = fib(n-1) + fib(n-2)
*/
public static int fib(int n) {
//Base case: n < 2
if (n < 2) {
return n;
}
else {
//Recursive case: fib(n) = fib(n-1) + fib(n-2)
return fib(n-1) + fib(n-2);
}
}
public static void main(String[] args) {
//0 1 1 2 3 5 8 13 21 34
//Running fibonacci series 0 to 9.
for (int i = 0; i < 10; i++) {
System.out.print(fib(i)+" ");
}
}
}