Question

The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

The Fibonacci sequence is the sequence of numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

The next number is found by adding up the two numbers before it.

For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called a term.

In this exercise, you will need to:

  1. Create the array int[] sequence that holds the values of the first 15 terms of the Fibonacci sequence. Think carefully about what happens to the index when iterating through the loop to fill this array. Read the Fibonacci description above to help!
  2. Then print out the sequence of numbers seperated by a space.
  3. Finally, create a method findIndex to find the index of the term 55.

Sample output:

Fibonacci sequence up to 15 terms:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 
Index position of 55 is: 10

Hint: You will need to use several loops: One to fill the array, one to print the array, and one to traverse the array!

Below is the coding given to change. Please use a template:

public class Fibonacci
{
public static void main(String[] args)
{

//number of elements to generate in the sequence
int max = 15;

// create the array to hold the sequence of Fibonacci numbers

//create the first 2 Fibonacci sequence elements
sequence[0] = 0;
sequence[1] = 1;

//create the Fibonacci sequence and store it in int[] sequence


//print the Fibonacci sequence numbers


System.out.println("\nIndex position of 55 is: " + findIndex(sequence, 55));

}

// This method finds the index of an element in an array

public static int findIndex (int[] arr, int n)
{

// your code goes here
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Fibonacci {
    public static void main(String[] args) {

//number of elements to generate in the sequence
        int max = 15;

// create the array to hold the sequence of Fibonacci numbers
        int[] sequence = new int[max];

//create the first 2 Fibonacci sequence elements
        sequence[0] = 0;
        sequence[1] = 1;

//create the Fibonacci sequence and store it in int[] sequence
        for (int i = 2; i < sequence.length; i++) {
            sequence[i] = sequence[i - 1] + sequence[i - 2];
        }

        System.out.println("Fibonacci sequence up to 15 terms:");
        for (int i = 0; i < sequence.length; i++) {
            System.out.print(sequence[i] + " ");
        }
        System.out.println();

//print the Fibonacci sequence numbers
        System.out.println("\nIndex position of 55 is: " + findIndex(sequence, 55));
    }

    // This method finds the index of an element in an array
    public static int findIndex(int[] arr, int n) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == n)
                return i;
        }
        return -1;
    }
}
Add a comment
Know the answer?
Add Answer to:
The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Fibonacci numbers – for loops We’ll define the Fibonacci numbers to be an integer sequence starting...

    Fibonacci numbers – for loops We’ll define the Fibonacci numbers to be an integer sequence starting with 1, 1, and where each subsequent element is got by adding the previous two elements: 1, 1, 2, 3, 5, 8, . . . (Mathematicians start with 0, 1, . . . , but we want to avoid zeros because you can’t take their log. ) Write a python programme in a Jupyter notebook which uses a for-loop to fill an array with...

  • A Fibonacci sequence is a series of numbers where the next number in the series is...

    A Fibonacci sequence is a series of numbers where the next number in the series is the sum of the previous two numbers. The sequence starts with the numbers 0 and 1. Here are the first ten numbers of the Fibonacci sequence:             0 1 1 2 3 5 8 13 21 34 Write a Java program to print out the first fifteen numbers of the Fibonacci sequence using a loop. You will need three variables previous, current, and next...

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by...

    this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1,1 and proceeds as follows: 1, 1, 2, 3, 5, 8, 13, . However, a Fibonacci sequence can be created with any two starting numbers. Create a MATLAB function called FL_fib_seq' (where F and L are your first and last initials) that creates a Fibonacci sequence. There should be three inputs. The first...

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .......

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... It is defined by the following mathematical expression, with X0 & X1 being 0 and 1, respectively: Xn = Xn-1 + Xn-2 Write a C program using the fork() system call that generates and prints the Fibonacci sequence in the child process. The number of members in the sequence will be determined by a user provided as a user prompted input. Make the parent...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence...

    Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence of numbers where each number is represented by the sum of the two preceding numbers, starting with 0 and 1: 0, 1, 1, 2, 3, 5, 8, etc Write a program that repeatedly prompts the user for a positive integer and prints out whether or not that integer is a Fibonacci number. The program terminates when-I is entered. Create a method with the following...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT