Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program Fills a one-dimensional array with the first 20 Fibonacci numbers us

ID: 3848900 • Letter: T

Question

The program Fills a one-dimensional array with the first 20 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two elements (0 and 1). From that point on the numbers must be calculated. The program prints the first 20 Fibonacci numbers in the array separated by a comma and a space. Asks the user which of the numbers in this series of 20 he wants to see and prompts for an integer input - a number between 1 and 20(inclusive). So if the user wants to see the fifth (5th) number of the Fibonacci series the user would input the integer 5 in response to the prompt. Checks that the user has not input a number lower than 1 or higher than 20 Prints in response to the user entry "The nth Fibonacci number is X", where n is the number input by the user and X is the nth Fibonacci number. (Array indexes for the elements of the array start at 0), Example: If user inputs "6" in response to the prompt, the program would print "The 6th Fibonacci number is 5." (without the quotes) You can check if you have the right numbers here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html (Links to an external site.)Links to an external site. Submission: Submit your work in a folder labeled XXXFibonacci where the XXX stands for the initials of your first, middle and last names. Compress the folder as usual using the .zip utility of your operating system. Make sure your first comment in your code is similar to the first commment in the lewis and Loftus programs in your text. The comment should include the class name, your name and section number at a minimum. //****************************************************************************************** // Fibonacci.java Author: Your Name Section: Your section number // // Representsthe first n integers of the Fibonacci series and prints them // then prints the nth number selected by the user //*****************************************************************************************

Explanation / Answer

Fibonacci.java

package a8;

import java.util.Scanner;

public class Fibonacci {
   public static void main(String arg[]) {
       int limit = 20;
       long[] series = new long[limit];
       series[0] = 0;
       series[1] = 1;
       for(int i=2;i < series.length;i++) {
           series[i] = series[i-2]+series[i-1];
       }
       System.out.println("Ffirst 20 Fibonacci numbers in the array: ");
       for(int i=0;i<series.length; i++) {
           if(i == series.length-1){
               System.out.print(series[i]);
           } else {
           System.out.print(series[i]+", ");
           }
       }
       System.out.println();
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter a number between 1 and 20(inclusive): ");
       int n = scan.nextInt();
       if(n < 1 || n >20) {
           System.out.println("Invalid input. Number must be between 1 and 20");
       } else {
           System.out.println("The "+n+"th Fibonacci number is "+series[n-1]+".");
       }
   }
}

Output:

Ffirst 20 Fibonacci numbers in the array:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
Enter a number between 1 and 20(inclusive): 6
The 6th Fibonacci number is 5.