# Fibonacci

• 12-22-2010
Paged
Fibonacci
Hey guys, I'm fairly new to coding, in fact, this is the second program I've ever tried haha. I can't quite work out why my loop is terminating early in this program:

Code:

```#include <stdio.h> int main () {     int fibonacci, fib3, fib2, fib1;         printf("What number would you like the series to go to?");                 scanf("%d", &fibonacci);         printf("%d\n", fibonacci);         fib2=1;         fib1=0;                 if (fibonacci = 0) {                 printf("The series is 0");                 return 0;         }         do {                         fib3 = fib2 + fib1;                         printf("%d+\n",fib3);                         fib1=fib2;                                fib2=fib3;                }         while (fib3 <= fibonacci);                                 return 0; }```
All I get as an output, when say putting in 9 as the input, is this:

Quote:

What number would you like the series to go to?9
9
1+
Obviously it's going through the loop once, before deciding it's ready to terminate without going through again. I can't work out why it wants to terminate without looping through twice. Any help you could offer would be great, thanks!
• 12-22-2010
tabstop
What does this bit of code do?
Code:

`fib1=0;`
That's right, it sets the value of fib1 to be zero. Now what about this bit:
Code:

`fibonacci = 0`
• 12-22-2010
anduril462
Code:

`if (fibonacci = 0)`
Add a second = in there. With a single equals, you actually assign 0 to fibonacci, and the result is the value of fibonacci, which is 0. This causes your if statement to skip the special zero case.

You should have caught that if you had all the warnings turned up on your compiler, which is a very, very good idea to do, especially if you're new.