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
  1. A. 4
  2. B. 0
  3. C. 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic