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>
usingnamespace std;
int main()
{
unsignedlongint 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).