Write a recursive method factorial that takes an
int argument n and returns n! (n
factorial). Recall that 0! = 1 and
n! = n * (n - 1)!. Remember, recursive means no
for or while loops.
Write a recursive method fibonacci that takes an
int argument n and returns the
nth Fibonacci number. The Fibonacci sequence starts
0, 1, 1, 2, 3, 5, 8, 13, ... where each number is the sum
of the two preceding ones. That is, fibonacci(0) = 0,
fibonacci(1) = 1, and
fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2).
Write a recursive method sumDigits that takes an
int argument n and returns the sum of its
digits. For example, sumDigits(123) should return
6. Hint: n % 10 gives you the last digit and
n / 10 gives you the number without its last digit.
Write a recursive method power that takes two
int arguments base and exp and
returns base raised to the power exp without
using Math.pow. Recall that
base0 = 1 and
baseexp = base * baseexp - 1.
Write a recursive method countDigits that takes an
int argument n and returns the number of
digits in n. For example, countDigits(123)
should return 3 and countDigits(7) should
return 1. Hint: a number less than 10 has exactly one
digit.
Write a recursive method gcd that takes two
int arguments a and b and
returns their greatest common divisor using the Euclidean algorithm. The
key insight is that gcd(a, b) = gcd(b, a % b), and the
base case is gcd(a, 0) = a.