# Fibonacci sequence wont run.

• 08-08-2012
robinnbastar
Fibonacci sequence wont run.
This is the second problem from Project Euler. here the detail:
Quote:

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:
Quote:

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.

and here is my code in C:
Code:

```#include <stdio.h> int main() {     int sum = 0, a = 1, b = 2, c=0;     while(b<4000000)         {             c=a+b;             if ( c%2==0 )             {                 continue;                 if (c<4000000)                 {                     sum=sum+c;                 }             }             a = b;             b = c;                     }     sum=sum+1;     printf ("%d", sum);     return 0; }```
Compiling run without error, however when i run the program(.exe file) from command prompt, it do nothing. I dont know what to do next, since there is no error statement to look at.

Here the attachment if it help.
• 08-08-2012
stahta01
Why do you have a "continue;" statement?
I asking because it will likely result in a endless loop.

Tim S.
• 08-08-2012
cas
"Run" the program on paper. It will take almost no time to see what's going on. Note that "continue" means you want to restart the loop immediately.
• 08-08-2012
oogabooga
You have an extraneous "continue" statement messing up your control flow. Get rid of that.

You aren't summing in the value 2 (starting value of b).

You have a strange extra increment of sum just before you print it.

And "do not exceed four million" means b <= 4000000. (This would only make a difference if 4000000 happened to be a fib, but it's still the correct-to-the-letter code.)
• 08-08-2012
robinnbastar
okay so here the new code i got:
Code:

```#include <stdio.h> int main() {         int sum = 0, a = 0, b = 1, c = 0;         while(b<=4000000)                 {                         c=a+b;                         if ( (c%2==1) && (c<=4000000) )                         {                                 sum=sum+c;                         }                         a = b;                         b = c;                                         }         printf ("%d", sum);         return 0; }```
it finally run.

To everyone: since i want only even number, i try to tell the compiler to continue back to loop if c%2==0. I should have just use "!=" instead. and the increment of 1 at the end was for the fact that i start a=1 , b=2; so 1 was not in the final sum. Now i start a=0 , b= 1 and the final sum is no longer needed.

i still got the wrong result for the project, but that is for me to figure out the right algorithmic.
• 08-08-2012
Click_here
You may want to look into recursion
• 08-09-2012
laserlight
Quote:

Originally Posted by Click_here
You may want to look into recursion

An iterative solution is perfectly fine, and probably better, here.
• 08-09-2012
Click_here
Quote:

An iterative solution is perfectly fine, and probably better, here.
The more I think about it, the more I agree with you :)

Going through the numbers only once is a good thing.
• 08-09-2012
robinnbastar
I got it!!!!! Woooohooooo
they want the sum of even number, not odd number. so i change "c%2==1" to "c%2==0".
and it works. Off to problem number 3.