How to find the Fibonacci Sequence with C++?
<p>I am trying to solve problem #2 on Project Euler. The problem is: </p>

<p>"Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</p>

<p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</p>

<p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms."</p>

<p>I am trying to solve this in C++.</p>

<p>Here is my code so far</p>

<pre><code>#include &lt;iostream&gt;
 using namespace std;

int main()
{
    //ANSWER = 4613732
    int max = 4000000;
    int n;
    int sum;

    for(n=0; n&lt;=max;)
    {
        while(n%2 == 0)
        {
            n = (n-1)+(n-2);
        }
    }
    sum = n+=0;
    cout &lt;&lt; sum &lt;&lt; endl;
    return 0;
}
</code></pre>

<p>As you can see I know the correct answer from searching for it to check my answer. This code I have just runs forever and never shows an answer. Can anyone give me hints on how to arrive to this answer and to help improve my C++ code. Thanks in advance!</p>
