Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By considering the terms in the Fibonacci sequence whose values do not exceed four million,
- //find the sum of the even - valued terms.
- #include <iostream>
- using namespace std;
- int main()
- {
- long unsigned int x = 1, y = 2, sum = 2, ctr = 0;
- for (long unsigned int fibonacci = 3; fibonacci < 4000000; fibonacci = x + y)
- {
- if (fibonacci % 2 == 0)
- sum += fibonacci;
- if (ctr % 2 == 0)
- x = fibonacci;
- else
- y = fibonacci;
- ctr++;
- }
- cout << sum <<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement