THINK FIRST·CODE LATER

← All labs

Recursion: factorial, digits and Fibonacci

Problem

Write a class Recursion with three recursive methods (no loops):

public static long factorial(int n)
public static int digitSum(int n)      // 1234 -> 10
public static long fibonacci(int n)    // fibonacci(0) = 0, fibonacci(1) = 1

main reads one integer n and prints:

Factorial: 120
Digit sum: 15
Fibonacci: 5

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.