Explore Westonci.ca, the leading Q&A site where experts provide accurate and helpful answers to all your questions. Get quick and reliable solutions to your questions from knowledgeable professionals on our comprehensive Q&A platform. Connect with a community of professionals ready to help you find accurate solutions to your questions quickly and efficiently.

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 takes in an index, n, and returns the nth value in the sequence. Any negative index values should return -1.
Ex: If the input is: 7
the out put is :fibonacci(7) is 13
Note: Use recursion and DO NOT use any loops. // is this mean i don't have to use loops? for (int i = 1; i<= n; ++i)
import java.util.Scanner;
public class LabProgram {
public static int fibonacci(int n) {
/* Type your code here. */
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int startNum;
System.out.println("Enter your number: ");
startNum = scnr.nextInt();
System.out.println("fibonnaci(" + startNum + ") is " + fibonacci(startNum));
}
}

Sagot :

Answer:

if this helps you,do consider marking my ans as brainliest.

Explanation:

import java.util.Scanner;

public class LabProgram{

public static int fibonacci(int n) {

/* Type your code here. */

if(n<0)

return -1;

if(n==0||n==1)

return n;

else

return fibonacci(n-1)+fibonacci(n-2);

}

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int startNum;

System.out.println("Enter your number: ");

startNum = scnr.nextInt();

System.out.println("fibonnaci(" + startNum + ") is " + fibonacci(startNum));

}

}

Recursions involve calling a function from the function itself, until a condition is met.

The required function, where comments are used to explain 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 series is printed.

Read more about similar programs at:

https://brainly.com/question/24212022

Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. We appreciate your visit. Our platform is always here to offer accurate and reliable answers. Return anytime. Thank you for trusting Westonci.ca. Don't forget to revisit us for more accurate and insightful answers.