Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Adding Up in a Loop

Loops can do more than print. Very often, they add up many values. According to a famous story, the young Carl Friedrich Gauss, later a great mathematician, had to add up all the numbers from 1 to 100 at school. A loop does it in a moment:

int total = 0;
for (int i = 1; i <= 100; i++) {
    total += i;
}
printf("Total: %d\n", total);
Total: 5050

The variable total collects the sum. It starts at 0, because before you add anything, the sum is zero. In each iteration, total += i adds one number: after the first iteration, total is 1, then 3 (1 + 2), 6 (1 + 2 + 3), 10, and at the end 5050.

Where each line goes matters:

  • int total = 0; goes before the loop, so that it runs only once. Inside the loop's block, it would create a new total with the value 0 in every iteration. And after the loop, total wouldn't exist at all: a variable created in a block exists only in that block, just like the i of a for loop. The compiler would report error: use of undeclared identifier 'total'.
  • printf goes after the loop, when the sum is complete. Inside the loop, it would print every partial sum: 1, 3, 6, 10 and so on, a hundred lines.

The average is the sum divided by the number of values. Watch out for integer division. Here is the average of the numbers from 1 to 10:

int total = 0;
for (int i = 1; i <= 10; i++) {
    total += i;
}
double average = total / 10;
printf("Average: %.1f\n", average);
Average: 5.0

The sum is 55, so the real average is 5.5. But both total and 10 are whole numbers, so / divides them as whole numbers and drops the .5, just like 7 / 2 gives 3. Storing the result in a double doesn't help, because by then the division is already done. Convert the sum to a double before dividing:

int total = 0;
for (int i = 1; i <= 10; i++) {
    total += i;
}
double average = (double)total / 10;
printf("Average: %.1f\n", average);
Average: 5.5

(double)total turns the sum into a decimal number, and a decimal number divided by a whole number gives a decimal number. Put (double) right before total. (double)(total / 10) would only convert the result of the integer division, and you would get 5.0 again.

Instructions

  1. 1

    You're doing a six-day push-up challenge: 5 push-ups on day 1 and 5 more every day after that, as the program in the editor prints. How many push-ups do you do in all? Before the loop, create a variable total with the value 0. In the loop, add the day's push-ups, day * 5, to it with +=. After the loop, print the sum as Total: 105.

  2. 2

    At the end of the program, create a variable average of type double with the average number of push-ups per day: total divided by 6. Print it with %.1f as Average: 17.5. If your program prints Average: 17.0, read the part about integer division again.

Back

5/11

  1. 1. The while Loop
  2. 2. Infinite Loops
  3. 3. The for Loop
  4. 4. Counting Down and in Steps
  5. 5. Adding Up in a Loop
  6. 6. Counting
  7. 7. The break Statement
  8. 8. The continue Statement
  9. 9. Nested Loops
  10. 10. The do-while Loop
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.