Write a recursive method definition for a static method that has one parameter n of type int and that returns the n th Fibonacci number. The Fibonacci numbers are is 1, is 1, is 2, is 3, is 5, and in general
= + for i = 0, 1, 2, ...
Place the method in a class that has a main that tests the method.
import java.util.*;
public class FibonacciTest
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number:");
int n = input.nextInt();
System.out.println("Febonnacci number result: " + fibonacci(n));
}
public static int fibonacci(int f)
{
if (f == 0)
return 1;
else if (f == 1)
return 1;
else
return fibonacci(f - 1) + fibonacci(f - 2);
}
}
Result:
Enter number:15
Febonnacci number result: 987