ProgrammingHard
A loop computes the number of decimal digits of n = 10^18 with while (n) { n /= 10; cnt++; }. What value does cnt reach?
- A. 18
- B. 19
- C. 20
- D. 1
Correct answer: B. 19
10^18 has 19 digits, and each division removes one digit until n becomes 0. The loop runs once per digit.
More C Programming questions
- Trial division factorises a number up to 10^12 with for (i = 2; i i <= n; i++) while (n % i == 0) n /= i;. About how many outer iterations…
- A 2 x 2 matrix is raised to the power 10^18 by repeated squaring. How many squaring steps are needed?
- Which statement about the nested loop for (i = 0; i < n; i++) for (j = 1; j < n; j = 2) is correct?
- Which operation is O(1) in a C array, but O(n) in a singly linked list when the head is given?
- A program reads a number n and then loops for (i = 0; i < n; i++) with n = 10^9 and a constant body. Roughly how long does it take in C…
- Which operation makes a sorted array of n elements support lookup in O(log n) and insertion in O(n)?