14. Recursion
Activity 1: Calculating the Fibonacci Sequence
Solution
- Create a class named
Fibonacci
. - Create a static method called
fibonacci
to calculate the Fibonacci sequence for a given number. If the input number is greater than1
, this method will call itself.public static int fibonacci(int number) { if (number == 0) { return number; } else if (number == 1) { return 1; } else { return (fibonacci(number - 1) + fibonacci(number - 2)); } }
If the input number is
0
or1
, this method returns the input number (0
or1
, respectively...