import java.util.Scanner; public class Fib0 { public static int fib(int n) { int sol1, sol2, mySol; /* ============================= Base cases ============================= */ if ( n == 0 ) { return 1; // f0 = 1 } else if ( n == 1 ) { return 1; // f1 = 1 } else /* Recursive solver */ { sol1 = fib( n-1 ); // Solve smaller problem 1 sol2 = fib( n-2 ); // Solve smaller problem 2 mySol = sol1 + sol2; // Use solution to solve orig. proble, return ( mySol ); // Claim credit... } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, r; while ( true ) { System.out.print("n = "); n = in.nextInt(); r = fib(n); System.out.println("fib(" + n + ") = " + r); } } }