The average of all numbers

Report a typo

Write a program that does the following:

  1. Read two integers, a and b, from the user input. These numbers define the range [a, b] (inclusive).

  2. Find all numbers within this range (including a and b) that are divisible by 3.

  3. Calculate and print the mean (average) of these numbers.

Example: If the input range is [-5, 12], the numbers divisible by 3 are: -3, 0, 3, 6, 9, 12. The mean would be

3+0+3+6+9+126=276=4.5\frac{-3 + 0 + 3 + 6 + 9 + 12 }{6} = \frac{27}{6} = 4.5

Notes:

  • The input range will always contain at least one number divisible by 3.

  • Include both a and b in your calculations if they are divisible by 3.

To calculate the mean, sum all integers divisible by 3 within the range, then divide this sum by the count of these integers. Consider using a separate counter variable, initialized to 0, to keep track of how many numbers are divisible by 3. Increment this counter each time you encounter such a number. This counter is a divisor when calculating the final mean.

Sample Input 1:

-5
12

Sample Output 1:

4.5
Write a program in Python 3
# put your python code here
___

Create a free account to access the full topic