-2

I'm trying to get the sum of all the even Fibonacci numbers. I can print the numbers out but I can't get the sum of them. This is in java.

class driver {
    public static void main(String [] args) {
        int a; 
        int b = 0;
        int c = 1;

        for (int i = 0; i < 10; i++) { // Finds fibonacci sequence
            a = b;
            b = c;
            c = a + b;

            if ( c % 2 == 0) { // Check if it's even
                int sum = 0;
                sum = sum + c;
                System.out.println(sum);
            }
            else {

            }
        }
    }
}
Perdomoff
  • 938
  • 2
  • 7
  • 26
alex23434
  • 389
  • 5
  • 12
  • 2
    Take the print statement outside the loop. – sam Nov 02 '15 at 16:10
  • you keep setting the sum to `0` inside your loop every time you find an even, so effectively the code is simply `sum = c`. e.g. move the `int sum = 0` outside of the loop. – Marc B Nov 02 '15 at 16:10
  • Look at where you're initializing ```sum```; step through the code yourself and see why that is a problem. – Caleb Brinkman Nov 02 '15 at 16:11
  • Even terms will just keep on coming, so the sum of all even fibonacci terms is infinity. – NickJ Nov 02 '15 at 16:18

5 Answers5

6

You should not reinitialize the sum every time inside the loop, because that will cause it to lose the value it has. It should have its scope outside the if so that the same variable can be incremented each time.

class driver {
    public static void main(String [] args) {
        int a; 
        int b = 0;
        int c = 1;
        int sum = 0;

        for (int i = 0; i < 10; i++) { // Finds fibonacci sequence
            a = b;
            b = c;
            c = a + b;

            if (c % 2 == 0)  // Check if it's even
                sum += c;
        }
        System.out.println(sum);
    }
}
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
2
public static void main(String [] args) {
    int a; 
    int b = 0;
    int c = 1;
    int sum = 0;

    for (int i = 0; i < 10; i++) { // Finds fibonacci sequence
        a = b;
        b = c;
        c = a + b;

        if ( c % 2 == 0) { // Check if it's even
            sum += c;
        }
    }
    System.out.println(sum);
}

You originally set sum = 0 every single time 'c' was an even number. This code should work as sum = 0 only before the process begins. This is actually a common mistake, but you will quickly learn that it is a bad idea.

Just so you know, I put the System.out.println(sum) statement outside of the loop so you don't have 10 different numbers as output.

Brandon Willis
  • 145
  • 2
  • 15
  • When you use 4 million as the value for the fibonacci sequence, it gives you a negative number. This cannot be the right answer. – alex23434 Nov 04 '15 at 15:57
1

Declare the integer variable sum, outside the for loop. That should do it.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
1
public final class eu_p002_sol {  

    public static void main(String[] args) {  
        System.out.println(new eu_p002_sol().run());  
    }  
public String run() {  
        int sum = 0;  
        for (int x = 0; ; x++) {  
            int fib = fibonacci(x);  
            if (fib > 4000000)  
                break;  
            if (fib % 2 == 0)  
                sum += fib;  
        }  
        return Integer.toString(sum);  
    }  

    private static int fibonacci(int n) {  
        if (n < 0 || n > 46)  
            throw new IllegalArgumentException();  
        int a = 0;  
        int b = 1;  
        for (int i = 0; i < n; i++) {  
            int c = a + b;  
            a = b;  
            b = c;  
        }  
        return a;  
    }  
}  

Try this

0

maybe someone is still searching for a right answer:

static int sum = 0, seq = 0;

public static void main(String[] args) {

    fib(1,2);

}

static void fib(int i, int j) {

    seq = j;
    if (seq <= 4000000) {
        if (j % 2 == 0) {
            sum += j;
        }
        fib(j, i + j);
    } else {
        System.out.println(sum);
    }
}