Fibonacci sequence (recursion): The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the s

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Fibonacci sequence (recursion): The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the s

Post by answerhappygod »

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)); }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply