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 <stdio.h>
#include <math.h>
int main(void)
{
int x, y, z;
x=1;
y=0;
z=0;
printf("%d\n", x);
while (x<=1000000)
{
y=x+z;
z=y+x;
x=y+z;
printf("%d\n", y);
printf("%d\n", z);
printf("%d\n", x);
}
return (0);
}
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 <stdio.h>
#include <math.h>
int main(void)
{
int x, y, z;
x=1;
y=0;
z=0;
printf("%d\n", x);
while (x<=1000000)
{
y=x+z;
z=y+x;
x=y+z;
printf("%d\n", y);
printf("%d\n", z);
printf("%d\n", x);
}
return (0);
}