0

Here is the code:

class Fibonacci {
    static final int MIN_INDEX = 1;
    public static void main (String[] args){
        int high = 1;
        int low = 1;
        String jel;
        System.out.println("9: " + high);

      for (int i = 8; i >= MIN_INDEX; i--){
        if (high % 2 == 0)
          jel = " *";
        else 
          jel = " ";
        System.out.println(i + ": " + high + jel);
        high = low + high;
        low = high - low;
      }
   }
}

I want to write this program, to store the Fibonacci sequence in an array, and then write out them. But I can't write it. What can I do? I don't need to "mark them" with an *.

5
  • 3
    Is this homework? If so, add the homework tag, please. Commented Apr 11, 2011 at 18:56
  • 2
    It almost has to be homework, doesn't it? Commented Apr 11, 2011 at 18:59
  • Isn't this the same as your previous question, linked under RELATED ? Commented Apr 11, 2011 at 19:00
  • I ran it and got this output: 9: 1 8: 1 7: 2 * 6: 3 5: 5 4: 8 * 3: 13 2: 21 1: 34 * Is this what you want? Commented Apr 11, 2011 at 19:00
  • 1
    Could you be a little more specific? Are you having problems declaring an array? Writing out data contained in an array? Understanding what an array has to do with the Fibonacci sequence? It sounds like you genuinely want assistance, but the way your question is written now, it looks like a "write some code for me" post, which isn't really what we do here. Commented Apr 11, 2011 at 19:00

3 Answers 3

1

Here are the steps

  1. Calculate the Fibonacci Sequence numbers that you need; store each value.
  2. Print the values that you calculated.

Calculate and store the Fibonacci Numbers

The Fibonacci sequence is a recursive function, but it would be ridiculous to implement it in any probramming language using recursion. Instead use a loop. For example:

int[] fibonacciNumbers = new int[20];
fibonacciNumbers[0] = 0;
fibonacciNumbers[1] = 1;

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

Print the array

Loop through each element in the array and print it however you want; for example, you could System.out.println(fibonacciNumbers[index]);

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

Comments

0

I think this will answer your question (I also added handling for some pessimistic scenarios):

public class Fibonacci
{
    public static void main(String[] args)
    {
        int[] fibMembers = buildFibArray(9);
        printFibArray(fibMembers);
    }

    private static void printFibArray(int[] fibMembers)
    {
        for (int i = 0; i < fibMembers.length; i++)
        {
            System.out.print(fibMembers.length-i);
            System.out.print(": ");
            System.out.print(fibMembers[i]);
            if (fibMembers[i] % 2 == 0)
            {
                System.out.print(" *");
            }
            System.out.println("");
        }
    }

    private static int[] buildFibArray(int maxIndex)
    {
        int[] fibMembers = new int[maxIndex];

        if (maxIndex > 0)
        {
            fibMembers[0] = 1;
            if (maxIndex > 1)
            {
                fibMembers[1] = 1;

                for (int i = 2; i < fibMembers.length; i++)
                {
                    fibMembers[i] = fibMembers[i-2] + fibMembers[i-1];
                }
            }
        }
        return fibMembers;
    }
}

The buildFibArray method builds the array.
The printFibArray method prints the array - should be according to your requirements.

Comments

0

If you want only pair Fibonacci numbers :

public class Fibonacci {       

    private int count = 0;

    public Integer next() {
        return fib(count++);
    }

    private int fib(int n) {
        if (n < 2) {
            return 1;
        }
        return fib(n - 2) + fib(n - 1);
    }

public static void main(String[] args) {
    List<Integer> pairFib = new ArrayList<Integer>();
    Fibonacci gen = new Fibonacci();
    for (int i = 0; i < 18; i++) {
        int current = gen.next();
        if (current % 2 == 0) {
            pairFib.add(current);
        }
    }
    System.out.println(pairFib);
}

}

1 Comment

if you replace your recursive function call by an array reference, this will give the OP what he/she wants

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.