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...


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 andDO 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));
   }
}



Jun 02, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here