Fibonacci sequence (recursion):
The Fibonacci sequence begins with 0 and then 1 follows. Allsubsequent values are the sum of the previous two, for example: 0,1, 1, 2, 3, 5, 8, 13. Complete the fibonacci() method, which takesin an index, n, and returns the nth value in the sequence. Anynegative index values should return -1. Ex: If the input is: 7 theoutput is: fibonacci(7) is 13
Note: Use recursion and DO NOT use any loops.
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; startNum = scnr.nextInt(); System.out.println("fibonacci(" + startNum +") is " + fibonacci(startNum)); }}
Fibonacci sequence (recursion): The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the s
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am