the fibonacci sequence begins with 0 and then 1 follows. all subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. complete the fibonacci() method, which has an index, n, as parameter and returns the nth value in the sequence. any negative index values should return -1. The necessary function, which uses comments to describe each line, is as follows:
//This defines the function
public static int fibonacci(int n) {
//The function returns -1, if n is negative
if(n<0){
return -1;}
//The function returns the value of n, if n is 0 or 1
if(n==0||n==1){
return n;}
//If otherwise
else{
//The function returns the nth term of the Fibonacci series
return fibonacci(n-1)+fibonacci(n-2);
}
}
At the end of the program, the nth term of the Fibonacci sequence is displayed.
For more information on Fibonacci sequence, see:
Brainly.com/question/24212022
#SPJ4