Search⌘ K

Discussion: You See It Everywhere

Explore the Fibonacci sequence by running and analyzing C code that outputs its first 20 values. Learn how two variables and loop constructs work together to calculate the series, enhancing your grasp of algorithmic thinking and code execution in C.

Run the code

Now, it's time to execute the code and observe the output.

C++
#include <stdio.h>
int main()
{
int f, n;
int count;
f = n = 1;
count=0;
while( count < 20 ) {
printf("%d ", f);
f+=n;
printf("%d ", n);
n+=f;
count+=2;
}
putchar('\n');
return(0);
}

Understanding the output

The code outputs the first 20 values of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
Code output
...