Finding the sum of even valued terms in Fibonacci sequence

Basically what you're doing here is adding every second element of the fibonacci sequence while the question asks to only sum the even elements.

What you should do instead is just iterate over all the fibonacci values below 4000000 and do a if value % 2 == 0: total += value. The % is the remainder on division operator, if the remainder when dividing by 2 equals 0 then the number is even.

E.g.:

prev, cur = 0, 1
total = 0
while True:
    prev, cur = cur, prev + cur
    if cur >= 4000000:
        break
    if cur % 2 == 0:
        total += cur
print(total)

def fibonacci_iter(limit):
    a, b = 0, 1
    while a < limit:
        yield a
        a, b = b, a + b

print sum(a for a in fibonacci_iter(4e6) if not (a & 1))

Here is simple solution in C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=1,j=1,sum=0;
    while(i<4000000)
    {
    i=i+j;
    j=i-j;
    if(i%2==0)
    sum+=i;
    }
printf("Sum is: %d",sum);

}