#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Fibonacci Sequence (Language-c)

Member Posts: 4
I am asked to type a code of the Fibonacci sequence until it reaches a value less than or equal to 1 million. For those that don't know the sequence is so that you start at 1 and the next number is the 2 numbers prior added, ex... 1,1,2,3,5,8....

I have the jist of the program already BUT it doesn't never stops at the number before 1 million (which should be 832040) instead it goes either the one above 1 million or a couple numbers down from 1 million. No errors in my code just need clarification on how to make it stop at one million. Also thanks for the help but please don't suggest "easier" methods because the method I used is what I have learned so far.

[language-c]

#include
#include

int main(void)

{

int x, y, z;

x=1;
y=0;
z=0;

printf("%d
", x);
while (x<=1000000)
{

y=x+z;
z=y+x;
x=y+z;

printf("%d
", y);
printf("%d
", z);
printf("%d
", x);

}

return (0);

}

• Member Posts: 3
Just [b]add IF Statement[/b] into your [b]While LOOP before printing Value of X[/b].....This works better...Try it...

printf("%d
", y);
printf("%d
", z);

[size=3][color=Red][b]if(x<=1000000)[/b][/color][/size]
printf("%d
", x);

------------------------------
Happy Programming......:-)
------------------------------
• Member Posts: 3
Just [b]add IF Statement[/b] into your [b]While LOOP before printing Value of X[/b].....This works better...Try it...

printf("%d
", y);
printf("%d
", z);

[size=3][color=Red][b]if(x<=1000000)[/b][/color][/size]
printf("%d
", x);

------------------------------
Happy Programming......:-)
------------------------------
• Member Posts: 4
Ahh awesome it works perfect. Just curious, what was my initial problem.

Thanks a lot.
• Member Posts: 3
There is Nothing Like Problem in your code...but you just MiSS don't understand your Own statement....Read your question more than TWICE you'll Get it....

Happy Programming......:-)
• Member Posts: 67