Skip to main content

Algorithm for numbers divisible by 9 not 6

Short answer

Read numbers until user enters NO after at least 50 entries. For each integer in 1–100 exactly divisible by 9 but not by 6, accumulate sum and count. After input, if count > 0 compute average = sum/count; else display no‑such‑number message.

You are given a task to develop a system to read at least 50 integer numbers and continue until the user enters NO. Your system must have capacity to calculate the sum and average of those numbers which are exactly divisible by 9 but not by 6 and lies in between 1 to 100 and display a suitable message if no such number is read. Write algorithm and flowchart to develop such system.

Assumptions: numbers are read as text so that the word NO can be detected; at least 50 numbers must be entered before NO is accepted; a number qualifies if it is between 1 and 100, divisible by 9 and not divisible by 6 (these are 9, 27, 45, 63, 81, 99).

Variables: total (numbers read), count (qualifying numbers), sum.

Algorithm

  1. Start
  2. Set total = 0, count = 0, sum = 0
  3. Read input
  4. If input = NO: if total >= 50 go to step 8, otherwise print "Enter at least 50 numbers" and go to step 3
  5. Convert input to the integer n and set total = total + 1
  6. If n >= 1 and n <= 100 and n mod 9 = 0 and n mod 6 != 0, then set sum = sum + n and count = count + 1
  7. Go to step 3
  8. If count = 0, print "No such number was entered"
  9. Otherwise calculate avg = sum / count and print sum and avg
  10. Stop

Flowchart

Part 1: reading and testing the numbers

        ( Start )
            |
  [ total=0, count=0, sum=0 ]
            |
     +----->|<-----------------+ (B)
     |      v                   |
     | / Read input /           |
     |      |                   |
     | < input = NO ? >--Yes--> (A)
     |      | No
     | [ n = integer(input) ]
     | [ total = total + 1 ]
     |      |
     | < n>=1, n<=100, n%9=0, n%6!=0 ? >
     |    No |            | Yes
     |       |    [ sum = sum + n ]
     |       |    [ count = count + 1 ]
     +-------+------------+

Part 2: after NO is entered

    (A)
     |
 < total >= 50 ? >--No--> / Print "Enter at least 50" /
     | Yes                          |
     |                             (B) back to Read input
 < count = 0 ? >--Yes--> / Print "No such number" /
     | No                           |
 [ avg = sum / count ]              |
 / Print sum, avg /                 |
     |                              |
     +-------------+----------------+
                   |
               ( Stop )

Example: if the qualifying numbers entered are 9, 27 and 45, then sum = 81, count = 3 and average = 27.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 2 questions →