Home Segments Top Top Previous Next

452: Mainline

Capturing the Fibonacci rabbit formula in the form of a Java method, you have the following:

public class Demonstrate { 
 public static void main (String argv[]) { 
  System.out.print("At the end of month 3, there are "); 
  System.out.println(rabbits(3)); 
  System.out.print("At the end of month 10, there are "); 
  System.out.println(rabbits(10)); 
 } 
 public static int rabbits (int n) { 
  if (n == 0 || n == 1) {return 1;} 
  else {return rabbits(n - 1) + rabbits(n - 2);} 
 } 
} 
--- Result --- 
At the end of month 3, there are 3 
At the end of month 10, there are 89