Java examples for Object Oriented Design:Method Recursive
Recursive fibonacci method with BigInteger
import java.math.BigInteger; public class Main { private static BigInteger TWO = BigInteger.valueOf(2); // recursive declaration of method fibonacci public static BigInteger fibonacci(BigInteger number) {/*ww w .ja v a 2s . co m*/ if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE)) // base cases return number; else { return fibonacci(number.subtract(BigInteger.ONE)).add(fibonacci(number.subtract(TWO))); } } // displays the fibonacci values from 0-40 public static void main(String[] args) { for (int counter = 0; counter <= 40; counter++) System.out.printf("Fibonacci of %d is: %d%n", counter, fibonacci(BigInteger.valueOf(counter))); } }