0

i don't understand what is the way to access the data in the array and use it as a condition the condition is to stop looping after the content exceeds 4,000,000 and also store and add the value if its value is an even number!

    int[] a=new int[40];
    int add=0;
    a[0]=1;
    a[1]=2;
    int i=2;
    do{
        a[i]=a[i-1]+a[i-2];

        System.out.println(a[i]);

        if(a[i]%2==0)
        {
          add=add+a[i];
         }
        i++;
    }
    while(i<32);

    System.out.println(add);
5
  • Just out of curiosity, what is your motivation for summing all the even numbers below 4,000,000 in the Fibonacci sequence? I can think of few applications for this. Commented Feb 10, 2016 at 4:16
  • jus trying out to become a pro by answering some projecteuler website answers Commented Feb 10, 2016 at 4:17
  • Interesting web site, with progressively more challenging problems! Commented Feb 10, 2016 at 4:22
  • 1
    To make it even more challenging, try doing it in Shakespeare Programming Language! (progopedia.com/language/shakespeare) Commented Feb 10, 2016 at 4:29
  • You are already accesing the array in if(a[i]%2==0) you should make another array that only contains even fib numbers, to store them. Commented Feb 10, 2016 at 5:55

3 Answers 3

2

Just put a check for your variable add to see if its value is greater than 4,000,000 and break out of the loop. Do something like this:

if(add > 4000000) {
   break;
}

So your final code will look like this:

int[] a=new int[40];
    int add=0;
        a[0]=1;
        a[1]=2;
    int i=2;
    do{
        a[i]=a[i-1]+a[i-2];
        System.out.println(a[i]);
        if(a[i]%2==0){add=add+a[i];}
        if(add > 4000000) {
            break; //this will get you out of your loop
        }
        i++;
    }while(i<32);
    System.out.println(add);
Sign up to request clarification or add additional context in comments.

4 Comments

The while(i<32) will break the loop long before the sum reaches 4000000. Also, I suspect that the number he wants to compare is the Fibonacci sequence value, not the sum.
also an another problem is its not getting the value correctly in 'if(a[i]%2==0){add=add+a[i];}' its just summing up everything
That line ought to work correctly. But you are forgetting to include the first even number (2) in your sum.
Ya I just want to sum up the even numbers
0

I would just throw this out there: Using a do-while loop may be making this harder than it has to be. The first part of your code is totally reasonable, but the while i > 32 is less than clear.

I'd look at it this way. After you initialize your array and its first two values with

int[] a = new int[40];
a[0] = 1;
a[1] = 2;

You know that you've accounted for one even value (2). So just initialized add to 2.

Now for the loop. You want to start at i = 2, and iterate as long as add is less than or greater than 4,000,000, right? So make a for loop to express it:

for (int i = 2; add <= 4000000; i++) {
    a[i] = a[i - 2] + a[i -1];
    if (a[i] % 2 == 0) {
        add += a[i];
    }
}

No need for the i < 32, and no need for any break statements!

Comments

0

As a matter of interest, this is a good application for Java 8 streams.

class Fib implements IntSupplier {
    private int current = 1;
    private int previous = 0;

    public int getAsInt() {
        int next = current + previous;
        previous = current;
        current = next;
        return current;
}

IntStream.generate(new Fib())
    .limit(4000000)
    .filter(n - > n % 2 == 0)
    .sum();

Comments