0

I have the task of writing a program using the fibonacci sequence and putting them into arrays. It works by getting user input ( how many numbers in the sequence the user wants to print out) and then it implements that into an array and prints out the sequence with the number of 'numbers' the user inputed. As I missed out on 2 weeks of class I looked online on how to write this program and found a video which the following code was written. So I do not take credit for the following code, I'm merely using it as an example.

Anyway here's the code:

public class Fibonacci
{
    public static void main(String[] args)
    {
        int numToPrint;
        //how many numbers to print out

        Scanner scan = new Scanner(System.in);
        System.out.println("Hvað viltu prenta út margar tölur úr Fibonacci röðinni?");
        numToPrint = scan.nextInt();
        scan.close();

        //prints out the first 2 numbers
        int nuverandiT = 1;
        int lokaT = 0;
        System.out.println(lokaT);
        System.out.println(nuverandiT);

        //prints out the rest of the sequence
        int lokaLokaT;
        for(int i = 2; i < numToPrint; i++)
        {
            lokaLokaT = lokaT;
            lokaT = nuverandiT;
            nuverandiT = lokaLokaT + lokaT;
            System.out.println(nuverandiT);
        }
    }
}

Now this prints out the fibonacci sequence with input from the user, but I'm not quite sure how to make it print out into an array. Do any of you guys know how to do this?

1
  • What do you mean by print out into an array? Do you want the values stored in an array instead of printing them in the console? Or would you like the values printed in the console, but in the format "[2, 3, 5, 8]"? Commented Feb 20, 2014 at 19:43

2 Answers 2

1

You have to create an array, for example:

int[] simpleArray;
simpleArray = new int[numToPrint];

At the place of

System.out.println(lokaT);
System.out.println(nuverandiT);

Put:

simpleArray[0] = lokaT;
simpleArray[1] = nuverandiT;

And inside your loop, you put instead this:

System.out.println(nuverandiT);

This: simpleArray[i] = nuverandiT;

Sign up to request clarification or add additional context in comments.

3 Comments

In this case wouldn't you want to initialize the array as new int[numToPrint+2]?
let's say that numToPrint = 10, if you initialize new int[numToPrint], you will have 10 array positions (0 to 9). The for loop is until i < numToPrint AND the i starts at 2, going just until 9. So, that should be work without this +2
@user3333633 If this solution works for you, then click the check mark under the score to show that it is the accepted answer.
0

I'm guessing when you say 'print out into an array' you really mean you just want to store the values in an array. In that case,

Before your for loop:

int[] array = new int[numToPrint];

And inside your for loop:

array[i-2] = nuverandiT;

If you wanted to print the numbers once they've been stored in an array, you would probably want to loop through it and print in the same fashion, accessing the elements by index. For more information, the java documentation is very good. I recommend reading up on arrays and counted loops.

2 Comments

You can use Arrays.toString(array) to print the array. No need for an extra loop.
Also a good option, depending on how picky you are on formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.