ProgrammingHardTCS NQT · Accenture
Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single loop. What is printed?
Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single loop. What is printed?
count = 0
for n = 1 to 100
a = (n mod 3 == 0); b = (n mod 5 == 0)
if a xor b then count = count + 1
end for
Print count- A. 47
- B. 41
- C. 6
- D. 40
Correct answer: B. 41
Multiples of 3 up to 100 number 33 and multiples of 5 number 20, with 6 multiples of 15 in both, so exactly one holds for 27 + 14 = 41. Counting the union gives 47, counting both gives 6, and stopping the range at 99 gives 40.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count…
- The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed? rev = 0; m = N…
- An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers…
- Compute C(10, 3) modulo 7 using Pascal's rule C(n, k) = C(n-1, k-1) + C(n-1, k), reducing every entry mod 7. What is printed? build the…
- By Fermat's little theorem, for a prime p and a not divisible by p, the inverse of a is a^(p-2) mod p. The procedure below finds the…
- A pair (a, b) with 1 <= a < b <= 20 is called coprime if gcd(a, b) = 1. The procedure below counts coprime pairs. What is printed? count =…