Average Value of Even Numbers That Are Divisible by Three
Here’s the Python code for the problem to calculate the average of all even integers that are divisible by 3:
|
|
Explanation
- We initialize two variables
total_sum
andcount
to keep track of the total sum of numbers that meet the criteria and the count of those numbers. - We iterate through the
nums
array and check if a number is both even (num % 2 == 0
) and divisible by 3 (num % 3 == 0
). - If the number meets the criteria, we add it to
total_sum
and increment thecount
. - After the loop, if
count
is 0, it means no numbers met the criteria, so we return 0. - Otherwise, we return the integer division of
total_sum
bycount
, which gives the average rounded down to the nearest integer.