ProgrammingVery hardTCS NQT · Accenture
The procedure below counts the numbers from 1 to 1000 whose digit sum is exactly 5. It uses digit-sum by repeated division. What is printed?
The procedure below counts the numbers from 1 to 1000 whose digit sum is exactly 5. It uses digit-sum by repeated division. What is printed?
count = 0
for n = 1 to 1000
s = 0; m = n
while m > 0: s = s + m mod 10; m = m div 10
if s == 5 then count = count + 1
end for
Print count- A. 15
- B. 21
- C. 28
- D. 56
Correct answer: B. 21
Three-digit strings with leading zeros and digit sum 5 number C(7,2) = 21, and 1000 has digit sum 1, so the answer is 21. Testing digit sum 4 gives 15, digit sum 6 gives 28, and testing at most 5 gives 56.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below finds how many integers x with 1 <= x <= 200 satisfy x^2 mod 13 == 1. What is printed? count = 0 for x = 1 to 200 if…
- The procedure below computes the value of the sum 1^2 + 2^2 + ... + 10^2 using the formula n(n+1)(2n+1)/6 for n = 10, and then the loop…
- 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…
- 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…