ProgrammingHardTCS NQT · Accenture
Count the integers n with 1 <= n <= 500 that are divisible by 4 or by 6. The procedure below uses a single loop. What is printed?
Count the integers n with 1 <= n <= 500 that are divisible by 4 or by 6. The procedure below uses a single loop. What is printed?
count = 0
for n = 1 to 500
if n mod 4 == 0 or n mod 6 == 0 then count = count + 1
end for
Print count- A. 125
- B. 167
- C. 83
- D. 41
Correct answer: B. 167
Multiples of 4 number 125 and multiples of 6 number 83, with 41 multiples of 12 counted in both, so the union is 125 + 83 - 41 = 167. Counting only one of the two divisors gives 125 or 83, and using and gives only the multiples of 12.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Find the gcd of the array [84, 126, 210, 462] by folding the Euclidean gcd across the elements, starting from 0. The procedure below runs…
- The number of trailing zeros of 1000! equals the power of 5 in 1000!, which is the sum of floor(1000 / 5^k) for k = 1, 2, 3, ... The…
- The least common multiple of the integers 1 through 10 is computed by folding lcm(a, b) = a b div gcd(a, b) from L = 1. The procedure…
- Count the three-digit numbers (100 to 999) in which every digit is odd. The procedure below uses a loop over the numbers and checks each…
- Count the pairs (i, j) with 1 <= i <= j <= 10 for which the product i j is a perfect square. The procedure below does this with two nested…
- The procedure below finds the largest prime less than 100 by testing each candidate from 99 down to 2 for divisibility by 2 up to its…