---
title: "Sum and average until 0 is entered"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:sum-average-until-zero"
source: https://www.bctengineer.com/q/semester_1/computer_programming/sum-average-until-zero
site: BCT Engineering
---

# Sum and average until 0 is entered

Computer Programming · Chapter 5: Control statements

- Asked in: 2068 Shrawan, 8 marks
- Video: [8.6.Problem solving using sentinel-controlled while loop | Complete C programming course](https://www.youtube.com/watch?v=_6pHBcmUrWI) — Sunil Dhimal
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/sum-average-until-zero
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program to read values from user and find sum until user types 0, also display sum and average.

## Short answer

Read numbers in a loop, summing and counting until 0 is entered; then if count > 0 compute average = sum/count and display both with two decimals. Zero is not included.

## Answer

The program reads numbers in a loop, adds each to `sum` and counts them, and stops when 0 is entered. The average is `sum / count`. If 0 is typed first (no data), a message is displayed to avoid division by zero. The zero itself is not included in the sum or the count.

```c
#include <stdio.h>
int main(void)
{
    float num, sum = 0, avg;
    int count = 0;
    printf("Enter numbers (0 to stop):\n");
    while (1)
    {
        scanf("%f", &num);
        if (num == 0)
            break;
        sum += num;
        count++;
    }
    if (count > 0)
    {
        avg = sum / count;
        printf("Sum = %.2f\nAverage = %.2f\n", sum, avg);
    }
    else
        printf("No numbers were entered.\n");
    return 0;
}
```
Output for the input 4, 7, 10, 3.5, 0:
```text
Enter numbers (0 to stop):
Sum = 24.50
Average = 6.12
```

Here the sum is 24.5 for 4 numbers, so the average is 6.125. With gcc (glibc), `%.2f` prints this as 6.12, because 6.125 is stored exactly and the tie is rounded to even; some other compilers print 6.13.

#### Explanation
- `while (1)` loops until `break` is executed on reading 0.
- `count` keeps the number of values, and the check `count > 0` prevents division by zero.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the loop stop?
   A. When count reaches 10
   B. break when 0 is read
   C. When sum exceeds 0
   D. On negative input
   Answer: B. while(1) with break on 0. (Key point: Stopping)

2. Is the terminating 0 added to sum or counted?
   A. Yes, both
   B. Only sum
   C. No
   D. Only count
   Answer: C. Break occurs before sum and count update. (Key point: Zero excluded)

3. Why check count > 0?
   A. Prevent overflow
   B. Check input type
   C. Format output
   D. Prevent division by zero
   Answer: D. If 0 typed first, count is 0. (Key point: Division by zero)

4. Input 4, 7, 10, 3.5, 0 gives sum and count?
   A. 24.5 and 4
   B. 24.5 and 5
   C. 21 and 4
   D. 28.5 and 5
   Answer: A. Four numbers sum 24.5. (Key point: Average)

## Related in Computer Programming

- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Sum of digits until single digit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adigit-sum-single-digit) — Computer Programming, chapter 5, asked 2×
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Sum and average between M and N using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-sum-average-range) — Computer Programming, chapter 9
- [Count even and odd until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-even-odd-until-minus1) — Computer Programming, chapter 5
- [Count prime and composite until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-prime-composite-until-minus1) — Computer Programming, chapter 5

## Related in other subjects

- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
