ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The procedure below repeatedly replaces x by the sum of its decimal digits, starting from x = 2^15, until x is a single digit, and prints x. What is printed?

The procedure below repeatedly replaces x by the sum of its decimal digits, starting from x = 2^15, until x is a single digit, and prints x. What is printed?
x = 2^15
while x >= 10: x = sum of the digits of x
Print x
  1. A. 8
  2. B. 26
  3. C. 3
  4. D. 5

Correct answer: A. 8

2^15 = 32768 has digit sum 26, and 26 has digit sum 8. Taking one digit sum gives 26, repeatedly dropping the last digit gives 3, and counting the digits gives 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic