// Scott DeRuiter 7/16/01 // Fibonacci.java // Use recursion to calculate a Fibonacci number. // 7/20/2011 - Greenstein: Changed TextReader to Scanner import java.util.Scanner; public class Fibonacci { private long number; private int index; public Fibonacci ( ) { number = 0; index = 0; } public static void main ( String [] args ) { Fibonacci program = new Fibonacci ( ); program.GetIndex ( ); program.FindAndPrint ( ); } public void GetIndex ( ) { Scanner keyboard = new Scanner ( System.in ); do { System.out.print ( "\n\n\nEnter a Fibonacci index (1 to 25) -> " ); index = keyboard.nextInt ( ); } while ( index < 1 || index > 25 ); } public void FindAndPrint ( ) { number = FindFib ( index ); System.out.println ( "The Fibonacci number at index " + index + " is: " + number ); } public int FindFib ( int i ) { // System.out.println ( i ); if ( i == 1 || i == 2 ) return 1; else return ( FindFib (i - 2) + FindFib(i - 1) ); } }