ProgrammingModerate
The sum of the digits of n = 10^9 - 1 is computed with a loop that divides by 10 each time. How many loop iterations run?
- A. 9
- B. 10
- C. 8
- D. 1000000000
Correct answer: A. 9
Each n /= 10 removes one decimal digit, and 10^9 - 1 = 999999999 has nine digits, so the loop runs nine times. 10 counts the digits of 10^9 itself, and 1000000000 confuses the value with its digit count.
More C Programming questions
- To list all primes up to 10^7 in C with memory and time that fit, which is the best choice?
- For each of Q = 10^5 queries, a program sums an array prefix of length up to N = 10^5 by a loop. What is the total cost?
- A recurrence T(n) = T(n - 1) + n is solved by a program with a loop over the recursion. What is the total cost in terms of n?
- What is the total number of elements in a triangle of rows 1 to 10, where row k has k elements?
- A program runs a loop body 10^8 times and each iteration does one multiply and one add on ints. Roughly how long does this take on typical…
- A bit-packed bitset of 10^9 bits needs how much memory?