Skip to content
Worked Examples · Example 4.9

Q.Write pseudocode and draw flowchart to accept numbers till the user enters 0 and then find their average.

West Bengal WbchseTextbookSubjective· 3mImportance★★★★★est
39% · 14/36 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

When the number of inputs is not known in advance, a sentinel value (here 0) ends the loop: prime the first read before the loop, test the value, and re-read at the bottom so the sentinel is never added to the sum.

The idea. Contrast with the previous example: there the trip count (5) was known; here the user decides when to stop by typing 0. That calls for a sentinel-controlled loop. The classic shape is the priming read: input once before the loop, test, process, then input again at the end of the body. This guarantees the sentinel 0 is checked before it could be added to sum or counted.

Pseudocode:

Step 1: SET sum = 0, count = 0
Step 2: INPUT num                       // priming read
Step 3: WHILE num is not equal to 0, REPEAT Steps 4-6
Step 4:     sum = sum + num
Step 5:     count = count + 1
Step 6:     INPUT num                   // read the next number
Step 7: IF count > 0 THEN
            average = sum / count
            PRINT average
        ELSE
            PRINT "No numbers were entered"

Flowchart (nodes and arrows):

  1. Start (oval)
  2. ↓ sum = 0, count = 0 (rectangle)
  3. ↓ Input num (parallelogram)
  4. ↓ Decision (diamond): "num = 0?"
    • No → sum = sum + num (rectangle) → count = count + 1 (rectangle) → Input num (parallelogram) → arrow back to the decision
    • Yes → step 5
  5. ↓ average = sum / count (rectangle)
  6. ↓ Output average (parallelogram)
  7. ↓ Stop (oval)

Python implementation:

total = 0
count = 0
num = int(input("Enter number (0 to stop): "))
while num != 0:
    total = total + num
    count = count + 1
    num = int(input("Enter number (0 to stop): "))
if count > 0:
    print("Average =", total / count)
else: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.