ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
n = 10 ** 6
s = 0
i = 1
while i <= n:
    s += n // i
    i *= 10
print(s)
  1. A. 1111111
  2. B. 1111110
  3. C. 1000000
  4. D. 1111100

Correct answer: A. 1111111

The loop adds n // 10^k for k = 0 to 6. The terms are 1000000, 100000, 10000, 1000, 100, 10 and 1, which add to 1111111.

More Python Programming questions

Exams that ask this topic