/* File: FibReport.java - March 2018 */ package l04; /** * Compute and display the Fibonacci number of the input argument * using a recursive algorithm reporting the recursive calls. The * indentation of the resulting output shows the depth of the recursive * stack at each call. * * Sample usage: java l04.FibReport 10 * * Note, this is intended to illustrate a problem with recursion. * * This can be fixed iteratively, or using a more subtle recursive approach, * see other code in this package * * @author Michael Albert * */ public class FibReport{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); fib(n, ""); } public static int fib(int n, String indent) { int result = 0; if (n <= 1) { result = 1; } else { result = fib(n-1, indent + " ") + fib(n-2, indent + " "); } System.out.println(indent + "Fib(" + n + ") = " + result); return result; } }