While Loop Product in C

Level 286 keystrokes

Uses a while loop to multiply the numbers 1 through 4 together, which is 4 factorial, and prints 24.

C code

// n is the counter; product starts at 1, not 0, because multiplying by 0 would always give 0
1int n = 1, product = 1;
// Repeats for n = 1, 2, 3 and 4
2while (n <= 4) {
// Multiplies the running product by n: 1, 2, 6, then 24
3 product *= n;
// Moves to the next number; the loop ends when n becomes 5
4 n++;
5}
// Prints the product 1 * 2 * 3 * 4, which is 24
6printf("%d\n", product);

Output

24
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.

  • ;× 4
  • =× 4
  • ,× 2
  • "× 2
  • (× 2
  • )× 2
  • +× 2
  • {× 1
  • }× 1
  • *× 1

More C examples at level 2

← C typing practice