Hello! I asked a question on a non-recursive Fibonacci sequence, here is the que
ID: 3916631 • Letter: H
Question
Hello! I asked a question on a non-recursive Fibonacci sequence, here is the question:
QUESTION: Write anon-recursive method for computing Fibonacci number for a given integer n. Test your method for the value n=100.
________________________________________________________
I was given an output that didn't make sense, so I redid the formatting and got the code below, but it contains an error at the "fibo" method and an issue with parsing at the final curly brace- would someone be able to fix the formatting so that it compiles? Thank you!!
public class Fibonacci{
public static void main(String[] args){
int previous = 0;
int previousprevious = 1;
int value;
public void fibo(int n){
for(int i = 0; i< value ; i++){
value = previous;
previous = previousprevious;
previous += value;
return previousprevious;
}
System.out.println(fibo(100));
}
}
}
Explanation / Answer
I'm sorry to say that there are quite a few erros in the code, and its not a simple case of formatting. Additionally, fib(100) is too large to be held in either int or long int types. I have posted the corrected code below:
public class Fibonacci
{
public static int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
int previous = 1;
int previousprevious = 0;
int value;
for(int i = 3; i <= n; i++)
{
value = previous;
previous += previousprevious;
previousprevious = value;
}
return previous;
}
public static void main(String[] args)
{
System.out.println(fibo(10));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.