ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (Armstrong number check)?

What will be the output of the following pseudocode (Armstrong number check)?
Function isArmstrong(Integer n)
    String s = toString(n)
    Integer p = length(s)
    Integer total = 0
    for (each d in s)
        total = total + (digitValue(d) ^ p)
    end for
    return total == n
End Function

Print isArmstrong(153), isArmstrong(123)
  1. A. true true
  2. B. true false
  3. C. false true
  4. D. false false

Correct answer: B. true false

153 has 3 digits, and 1^3+5^3+3^3 = 1+125+27 = 153, matching n: true. 123 has 3 digits too, but 1^3+2^3+3^3 = 1+8+27 = 36, which does not equal 123: false.

More Pseudocode and Code Tracing questions

Exams that ask this topic