6.32 LAB: Fibonacci sequence (EO) 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: L Ise a for loon and DO NOT use recursion

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

My Issue: 

I don't get why this is illegal start of type (i<0; i < n; i++) as it's the standard of the for loop. I also don't understand how Public Static fibonnacci (int n) { is illegal as the public static is meant to make it accessible in the main method.

My code: 

import java.util.Scanner;

public class FibonacciSequence {

public static fibonnacci(int n) {
/* Type your code here. */ public static fibonnacci (int n) {
int num1=0; // My logic of the fibonnacci: 0 1 1 2 3 5 ,so 0(num1)+1(num2)=1(num3), 1(num1)+1(num2)=2(num3)
int num2=1;
int num3;
int sum=0;

if (n < 0) {
return -1;
}
for (i < 0; i < n; i++) {
sum = num1 + num2;
num2=num3;
num3=sum;
}
return sum;

}

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
FibonacciSequence program = new FibonacciSequence();
int startNum;

startNum = scnr.nextInt();
System.out.println("fibonnacci(" + startNum + ") is " + program.fibonacci(startNum));
}
}

FibonacciSequence.java:6: error: illegal start of expression
/* Type your code here. */ public static fibonnacci (int n) {
FibonacciSequence.java:6: error: invalid method declaration; return type required
/* Type your code here. */ public static fibonnacci (int n) {
FibonacciSequence.java:15: error: illegal start of type
for (i < 0; i < n; i++) {
FibonacciSequence.java:15: error: not a
statement
for (i < 0; i < n; i++) {
FibonacciSequence.java:15: error:
not a
statement
for (i < 0; i < n; i++) {
FibonacciSequence.java:15: error:
')' expected
for (i < 0; i < n; i++) {
FibonacciSequence.java:15: error:
';" еxpected
for (i < 0; i < n; i++) {
7 errors
Transcribed Image Text:FibonacciSequence.java:6: error: illegal start of expression /* Type your code here. */ public static fibonnacci (int n) { FibonacciSequence.java:6: error: invalid method declaration; return type required /* Type your code here. */ public static fibonnacci (int n) { FibonacciSequence.java:15: error: illegal start of type for (i < 0; i < n; i++) { FibonacciSequence.java:15: error: not a statement for (i < 0; i < n; i++) { FibonacciSequence.java:15: error: not a statement for (i < 0; i < n; i++) { FibonacciSequence.java:15: error: ')' expected for (i < 0; i < n; i++) { FibonacciSequence.java:15: error: ';" еxpected for (i < 0; i < n; i++) { 7 errors
6.32 LAB: Fibonacci sequence (EO)
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.
Transcribed Image Text:6.32 LAB: Fibonacci sequence (EO) 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education