ProgrammingHardTCS NQT · Infosys

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer total = 0, n
for (each i from 1 to 100)
    n = i
    while (n > 0)
        total = total + n mod 10
        n = n / 10
    end while
end for
Print total
  1. A. 5050
  2. B. 900
  3. C. 901
  4. D. 1000

Correct answer: C. 901

For 1 to 99, each digit 0–9 appears 10 times in the units place and 10 times in the tens place: 2 × 10 × 45 = 900. Adding 100 (digit sum 1) gives 901.

More Pseudocode and Code Tracing questions

Exams that ask this topic