import java.io.*;
public class Fibonacci
{
public static void main (String [] args)
{
ComputeFibonacci
compute = new ComputeFibonacci ();
compute.displayData
();
}
} // Fibonacci
class ComputeFibonacci
{
// Get the number to compute and display the
answer.
public void displayData ()
{
try
{
BufferedReader
stdin = new BufferedReader (new InputStreamReader (System.in));
System.out.print
("Enter number: ");
int number
= Integer.parseInt (stdin.readLine ());
// Call
the recursive method with a new array of the required size.
int result
= fib (number, new int [number+1]);
System.out.println
("Fibonacci Number " + number + " " + result);
} catch (IOException e) {System.out.println
("Data error.");}
} // displayData
// Compute the fibonacci number using an array,
answer, to store intermediate results.
private int fib (int number, int answer [])
{
int fibNumber;
System.out.println
("Answer " + number + " " + answer [number]);
// If the array
already has a value for number, return that as the result.
if (answer [number]
!= 0) return answer [number];
if ((number
== 0) || (number == 1)) return 1;
else
{
fibNumber = fib (number-1, answer) + fib (number-2, answer);
// Store the intermediate result in the array, answer.
answer [number] = fibNumber;
return fibNumber;
}
} // fib
} // ComputeFibonacci
// Fibonacci example that displays intermediate results.
import java.io.*;
public class Fibonacci
{
public static void main (String [] args)
{
ComputeFibonacci
compute = new ComputeFibonacci ();
compute.displayData
();
}
} // Fibonacci
class ComputeFibonacci
{
public void displayData ()
{
try
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter number: ");
int number = Integer.parseInt (stdin.readLine ());
int result = fib (number);
System.out.println ("Fibonacci Number " + number + " " + result);
} catch (IOException
e) {System.out.println ("Data error.");}
} // displayData
private int fib (int number)
{
System.out.println
("Number " + number);
if ((number
== 0) || (number == 1)) return 1;
else return
fib (number-1) + fib (number-2);
} // fib
} // ComputeFibonacci