*i need help with this Java Program!* A variation on the number sequence Fibonac
ID: 3806857 • Letter: #
Question
*i need help with this Java Program!*
A variation on the number sequence Fibonacci envisioned to solve that problem -- 0, 1, 1, 2, 3, 5... -- has become associated with definitions of beauty and ratios unrelated to mathematics (but as it turns out, very useful in Computer Science).
The problem reads:
Prompt the user for the number of values in Fibonacci the user wants to see. The sequence may be produced by using the last two values in the sequence to produce the next value. Start the sequence with 0 and 1. The next value in the sequence would be 1 (= 0 + 1). The next value would be 2 (= 1 + 1) and the value following would be 3 (= 1 + 2).
A typical session might look like this...
How many Fibonacci numbers do you want to see? 9
The Fibonacci sequence for 9 numbers is...
0, 1, 1, 2, 3, 5, 8, 13, 21
Explanation / Answer
Fibonacci.java
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int n1 = 0, n2 = 1, n3, i, count;
Scanner scan = new Scanner(System.in);
System.out.print("How many Fibonacci numbers do you want to see? ");
count = scan.nextInt();
System.out.println("The Fibonacci sequence for "+count+" numbers is...");
System.out.print(n1 + ", " + n2);
for(i = 2; i < count; ++i)
{
n3 = n1 + n2;
System.out.print(", " + n3);
n1=n2;
n2=n3;
}
}
}
Output:
How many Fibonacci numbers do you want to see? 9
The Fibonacci sequence for 9 numbers is...
0, 1, 1, 2, 3, 5, 8, 13, 21
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.