Fibonacci numbers help

The problem:
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:

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

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


I've done it right with the source code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    unsigned long int first = 1, second = 1, third = 2;
    cout << first << endl;
    cout << second << endl;
    cout << third << endl;
    while(1)
    {
        first = second;
        second = third;
        third = first + second;
        if(third > 4000000) break;
        cout << third << endl;
    };
};


but what is the right way to write the code? I mean the professional way? Im new in programming and I dont want to be stucked for being a stupid programmer.
You do not need semicolons after closing braces, for one. Two, your main() does not return anything, when it should be returning 0. Three, you do not need the if statement in order to break- try replacing the 1 in your while argument with (third < 4000000).
Last edited on
I would try to write the code so there is just a single cout statement.
Thanks for #1 and #2 but #3 makes the final number 5702887 and the rule is dont let it exceed 4000000.
it seems you still change the value of third even if you stop from cout it.
try
while((first + second) < 400000)
Last edited on
Topic archived. No new replies allowed.