ProgrammingVery hardCAT · MBA placement
Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop. What is printed?
Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop. What is printed?
count = 0
for n = 1 to 1000
if n mod 7 == 0 and n mod 11 != 0 then count = count + 1
end for
Print count- A. 130
- B. 922
- C. 142
- D. 12
Correct answer: A. 130
Multiples of 7 up to 1000 number 142, and 12 of them are also multiples of 77, so 142 - 12 = 130. Testing with or instead of and gives a huge count, forgetting the exclusion gives 142, and testing divisibility by 77 counts only the common multiples.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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 =…
- 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…