Fibonacci Sequence in C

Level 3119 keystrokes

Generates and prints the first 7 numbers of the Fibonacci sequence.

C code

// a and b hold the last two numbers; c will hold the next one
1int a = 0, b = 1, c;
// The first two numbers, 0 and 1, are printed directly
2printf("%d %d ", a, b);
// 5 more numbers, 7 in total
3for (int i = 0; i < 5; i++) {
// Next = sum of the last two, then shift: a takes b, b takes c (order matters)
4 c = a + b; a = b; b = c;
// Print 1, 2, 3, 5, 8
5 printf("%d ", c);
6}

Output

0 1 1 2 3 5 8
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 8
  • =× 6
  • ,× 5
  • "× 4
  • (× 3
  • )× 3
  • %× 3
  • +× 3
  • {× 1
  • }× 1

More C examples at level 3

← C typing practice