2) Task: Practice with FOR loops (10 pts) -Create a program that lists out Fibon
ID: 3601108 • Letter: 2
Question
2) Task: Practice with FOR loops (10 pts) -Create a program that lists out Fibonacci numbers based upon user input. The Fibonacci sequence is a series of values where every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4184, 6765, etc. a) Create a new package "task2" and a new class in that package "Fibonacci". Add the empty "main" method in your class. b) Problem: (No functions are needed for this problem) o main: In the main, prompt the user to enter the number of Fibonacci values to generate then read and save their input value. Using a FOR loop, generate n Fibonacci values, where 'n' is the value the user entered. The output should be formatted as follows: «user's number> Fibonacci numbers: , value>, , value>, Example: 12 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 Done!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.print(count+" Fibonacci numbers: ");
System.out.print(n1 + ", " + n2);
for(i = 2; i < count; ++i)
{
n3 = n1 + n2;
System.out.print(", " + n3);
n1=n2;
n2=n3;
}
System.out.println(" Done");
}
}
Output:
How many Fibonacci numbers do you want to see? 12
12 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
Done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.