For Loop Product in C++

Level 288 keystrokes

Multiplies the numbers 1 to 5 together in a for loop to compute 5! (the factorial of 5) and prints 120.

C++ code

// Starts at 1, because starting at 0 would make every product 0
1int product = 1;
// i takes the values 1, 2, 3, 4 and 5
2for (int i = 1; i <= 5; i++) {
// Multiplies product by i: 1, 2, 6, 24, 120
3 product *= i;
4}
// Prints 5! = 120
5cout << product << endl;

Output

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

  • ;× 5
  • <× 5
  • =× 4
  • +× 2
  • (× 1
  • )× 1
  • {× 1
  • }× 1
  • *× 1

More C++ examples at level 2

← C++ typing practice