**Meet people**who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

# Project Euler 2 0

Updated over 4 years ago (15 Feb 2013 at 12:26 PM)
recent activity

Language | C++ |

# 's

` 1/*`

2Even Fibonacci numbers

3Problem 2

4Each new term in the Fibonacci sequence is generated by adding the previous two

5terms. By starting with 1 and 2, the first 10 terms will be:

6

71, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

8

9By considering the terms in the Fibonacci sequence whose values do not exceed

10four million, find the sum of the even-valued terms.

11

12*/

13#include <iostream>

14using namespace std;

15

16int main ()

17{

18 // Declare some shit; start with first two terms in sequence before the loop

19 int i1 = 1;

20 int i2 = 2;

21 int i3 = 3;

22 int sum = i2;

23

24 // Loop through sequence until term <= 4e6, add to sum if term is even

25 while (i3 <= 4.0e6) {

26 i3 = i1 + i2;

27

28 if (i3 % 2 == 0) {

29 sum += i3;

30 }

31

32 i1 = i2;

33 i2 = i3;

34 }

35 cout << sum << endl;

36 return 0;

37}

Add a Comment