ProgrammingHardTCS NQT · Accenture
An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers from 100 to 500 (inclusive) are Armstrong numbers. What is printed?
An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers from 100 to 500 (inclusive) are Armstrong numbers. What is printed?
count = 0
for n = 100 to 500
s = 0; m = n
while m > 0: s = s + (m mod 10)^3; m = m div 10
if s == n then count = count + 1
end for
Print count- A. 4
- B. 0
- C. 3
- D. 231
Correct answer: A. 4
The three-digit Armstrong numbers are 153, 370, 371 and 407, so the count is 4. Using squares finds none in range, stopping at 399 drops 407 and gives 3, and testing s >= n counts far more numbers.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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 =…
- The sum of the divisors of N = 28 is computed by the procedure below. A number is perfect when its proper divisors (excluding N) add up to…
- The procedure below counts the digits of 2^10 by repeated division by 10. What is printed? N = 2^10; d = 0 while N > 0 N = N div 10; d = d…
- Euclid's algorithm finds gcd(48, 18) by replacing the pair (a, b) with (b, a mod b) until b is 0. The loop below counts how many times the…