0
import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i<count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i=0; i<count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}

This is my very simple Fib program, what i cant figure out is why it always stops one number short. For example:

run: Please enter number 6 1 1 2 3 5 8 BUILD SUCCESSFUL (total time: 5 seconds)

run: Please enter number 7 1 1 2 3 5 8 13 BUILD SUCCESSFUL (total time: 5 seconds)

I thought in my FOR loops it should be "(int i=2; i <= count;"

but when i put in greater than or equal to in both, or either FOR loop it gives me an error

Any suggestions? i know its something easy i'm overlooking

7
  • 4
    Be more specific than "it gives me an error". It's usually best to include a stack trace. Commented Sep 10, 2013 at 1:36
  • Also, please take care to format your posted code well, including using regular and consistent code indentation that makes sense. Posting a decent question, one that's easy to read and understand shows that you are taking your problem, this site and our help seriously. Commented Sep 10, 2013 at 1:37
  • We need more info of the given error... I don't have any so far. Please enter number... 20... 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 Commented Sep 10, 2013 at 1:47
  • This seems to be working correctly. Are you saying that when you enter "6", you want it to give 7 numbers; and when you enter "7" you want it to give 8 numbers, and so on? That would be an ODD thing to want - but if you really DO want that, then just add 1 on to whatever the user enters ( like count = in.nextInt() + 1;). Commented Sep 10, 2013 at 1:53
  • Well i thought that was right to, its giving me 6 elements, thats what i asked for but my prof gives this for an example laeding me to think mine should output 7 numbers when i put in 6... java Fibonacci 6 fibo(0) = 1 fibo(1) = 1 fibo(2) = 2 fibo(3) = 3 fibo(4) = 5 fibo(5) = 8 fibo(6) = 13 Commented Sep 10, 2013 at 1:55

5 Answers 5

1

Your code is giving correct output. but still if you need one more element try to initialize array with count + 1 and then have your loop running for i <= count

public static void main(String[] args) {


int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");

count = in.nextInt();

        int[] fib = new int [count+1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++){
            fib[i] = fib[i-1] + fib[i-2];
        }

         for(int i=0; i <= count; i++){
             System.out.print(fib[i] + " ");

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

2 Comments

just add +1 gives me this...run: Please enter number 6 1 1 2 3 5 8 0 BUILD SUCCESSFUL (total time: 45 seconds)
I guess you picked the code before it was edited. I just edited the code. Try using the edited code. Use <= in both for loops.
1

Arrays are zero-based. This means, that (assuming count = 5) if you have the following array:

int[] fib = new int[5];

then you can access fib[0], fib[1], fib[2], fib[3] and fib[4]. So

for (int i = 0; i < 5; i++) {
    System.out.print(fib[i] + " ");
}

would be fine. As it would access everything in fib, starting with index 0, and stopping with the last index smaller than 5, which is 4. However, if you do:

for (int i = 0; i <= 5; i++) {
    System.out.print(fib[i] + " ");
}

then you will access the last index smaller than OR EQUAL TO 5, which is 5. But, as stated before, fib[5] is invalid. That's what gives you your error.

Comments

1

A simpler solution is to avoid needing an array in the first place and you don't need to get the size right.

public static void main(String[] args) {
    System.out.println("Please enter a number");
    Scanner in = new Scanner(System.in);
    int count = in.nextInt();

    long a = 1, b = 1;
    for(int i = 0; i < count; i++) {
        System.out.print(a + " ");
        long c = a + b;
        a = b;
        b = c;
    }
    System.out.println();
}

Comments

0

There should be one more array element space for int fib[], thus the fib[count] could be stored.

import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count + 1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i = 0; i<= count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}

Comments

0
public class Fibonacci
{
 private int [] fibArray;

public Fibonacci()
{
}

public void Fibonacci()
{
    fibArray = new int[0];
}

public void setFibonnaci(int size)
{
    fibArray = new int[size];

    if(fibArray.length == 1)
    {
        fibArray [0] = 0;
    }

    else if(fibArray.length == 2)
    {
        fibArray[0] = 0;
        fibArray[1] = 1;
        fibArray[2] = 2;
    }
    else 
    {
        fibArray[1] = 1;
        fibArray[0] = 0;

        for(int x = 2; x < fibArray.length; x++)
        {
            fibArray [x] = fibArray[x-1] + fibArray[x-2];
        }
    } 
}

public int getSequence(int number)
{
    if(number -1 < fibArray.length)
    {
        return fibArray[number - 1];
    }
    return -1;
}

//check the test case for getFibo
public String toString()
{
    String output = "";
    for (int x = 0; x < fibArray.length; x++)
    {
        output += x + " - " + fibArray[x];
    }
    return output;
}

}

Late response but new to site and just trying to help. This fib class works 100%

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.