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. Ex: If the input is: 7 the output is: fibonacci(7) is 13 Note: Use a for loop and DO NOT use recursion. code: import java.util.Scanner; public class FibonacciSequence { public int fibonacci(int n) { /* Type your code here. */ } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); FibonacciSequence program = new FibonacciSequence(); int startNum; startNum = scnr.nextInt(); System.out.println("fibonacci(" + startNum + ") is " + program.fibonacci(startNum)); } }
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.
Ex: If the input is:
7
the output is:
fibonacci(7) is 13
Note: Use a for loop and DO NOT use recursion.
code:
import java.util.Scanner;
public class FibonacciSequence {
public int fibonacci(int n) {
/* Type your code here. */
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
FibonacciSequence
int startNum;
startNum = scnr.nextInt();
System.out.println("fibonacci(" + startNum + ") is " + program.fibonacci(startNum));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images