Skip to content

Commit df38bb1

Browse files
adding the fibonacci series to the Recursion package
1 parent df0c997 commit df38bb1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thealgorithms.Recursion;
2+
3+
/*
4+
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones,
5+
starting with 0 and 1.
6+
NUMBER 0 1 2 3 4 5 6 7 8 9 10 ...
7+
FIBONACCI 0 1 1 2 3 5 8 13 21 34 55 ...
8+
*/
9+
10+
public class FibonacciSeries {
11+
12+
static int fib(int n) {
13+
if (n == 0 || n ==1)
14+
return n;
15+
else
16+
return fib(n - 1) + fib(n - 2);
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println("Fibonacci = " + fib(1)); // Fibonacci = 1
21+
System.out.println("Fibonacci = " + fib(0)); // Fibonacci = 0
22+
System.out.println("Fibonacci = " + fib(14)); // Fibonacci = 377
23+
}
24+
}

0 commit comments

Comments
 (0)