How many digits
Inputs
n is a whole number from 0 to 1,000,000.
Your tasks
- Create a number
countthat starts at 0 and a copyrestofn. - Use a
whileloop that runs as long asrestis more than 0. Each round, throw away the last digit ofrest(whole-number divide by 10) and add 1 tocount. - Complete
digitCountso it returnscount. Return it, do not print it.
Example
With n = 4821, digitCount returns 4.
Rules
- 0 has one digit, so return 1 for 0.
- Do not turn the number into text.
Test data
TEST DATA 1: n is 4821
TEST DATA 2: n is 0
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.