Thursday, September 30, 2010

Fibonacci Calculator & Fibonacci Test




/* Fibonacci Calculator*/

public class FibonacciCalculator
{
//reverse declaration of method Fibonacci
public long fibonacci( long number )
{
if((number ==0)||(number ==1)) //base cases
return number;
else //recursion step
return fibonacci( number-1) + fibonacci(number-2);
}

public void displayFibonacci()
{
for (int counter=0; counter<=10; counter++)
System.out.printf("Fibonacci of %d is %d\n", counter, fibonacci(counter)
);
}
}

------------------------------------------------

/*Fibonacci Test */
public class FibonacciTest
{
public static void main(String args[])
{
FibonacciCalculator fibonacciCalculator=new FibonacciCalculator();
fibonacciCalculator.displayFibonacci();
}
}

No comments:

Post a Comment