For Loop Sum in C

Level 276 keystrokes

Uses a for loop to add the numbers 1 through 5 into sum and prints the total, 15.

C code

// sum must start at 0 before anything is added to it
1int sum = 0;
// i takes the values 1, 2, 3, 4 and 5
2for (int i = 1; i <= 5; i++) {
// Adds the current i to the running total
3 sum += i;
4}
// After the loop sum is 1 + 2 + 3 + 4 + 5 = 15, which is printed
5printf("%d\n", sum);

Output

15
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
  • =× 4
  • +× 3
  • "× 2
  • (× 2
  • )× 2
  • ,× 1
  • {× 1
  • }× 1
  • \× 1

More C examples at level 2

← C typing practice