Find the best solutions to your questions at Westonci.ca, the premier Q&A platform with a community of knowledgeable experts. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform.
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
Your visit means a lot to us. Don't hesitate to return for more reliable answers to any questions you may have. Thank you for visiting. Our goal is to provide the most accurate answers for all your informational needs. Come back soon. We're here to help at Westonci.ca. Keep visiting for the best answers to your questions.