In: Computer Science
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms.
Sample output:
How many terms would like printed out for the Fibonacci Sequence?
7
Fibonacci Series of 7 numbers:
0 1 1 2 3 5 8
//TestCode.java import java.util.Scanner; public class TestCode { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("How many terms would like printed out for the Fibonacci Sequence?"); int n = scan.nextInt(); System.out.println("Fibonacci Series of "+n+" numbers:"); if(n>=1){ System.out.print(0+" "); } if(n>=2){ System.out.print(1+" "); } if(n>=3) { int x = 0, y = 1, z; for (int i = 2; i < n; i++) { z = x + y; x = y; y = z; System.out.print(z+" "); } System.out.println(); } } }