Problem overview
The method digitSum receives a non-negative integer n up to one billion and returns the sum of its digits. For n = 472 the digits are 4, 7 and 2, so the answer is 13. For 1005 it is 6, because the zeros add nothing, and a single digit such as 7 is its own sum. For 0 the sum is 0.
Approach: peel off the last digit
Two integer operations take a number apart without converting it to text:
n % 10is the remainder after dividing by 10, which is the last digit:472 % 10is 2.n / 10with twointvalues drops the remainder, which removes the last digit:472 / 10is 47.
Repeat both on the shrinking number. Each pass adds one digit to a running total and removes it from n. When n reaches 0, every digit has been counted.
Step by step
- Create
totalas anintstarting at 0. It holds the sum so far. - Write a
while (n > 0)loop. It runs once per digit. Forn = 0it never runs, and the answer 0 is already correct. - Add the last digit with
total += n % 10. - Drop the last digit with
n = n / 10. Without this linennever changes and the loop never ends. - Return
totalafter the loop.
The solution in Java
class Solution {
public int digitSum(int n) {
int total = 0;
while (n > 0) {
total += n % 10;
n = n / 10;
}
return total;
}
}
For n = 472, the loop passes look like this:
| Pass | n at the start | Digit added | total after |
|---|---|---|---|
| 1 | 472 | 2 | 2 |
| 2 | 47 | 7 | 9 |
| 3 | 4 | 4 | 13 |
After the third pass n is 0, the loop ends, and the method returns 13.
Java details:
- Integer division. When both operands are
int,/gives anintand throws away the fraction, so47 / 10is 4, not 4.7. - The parameter is a local copy. Changing
ninside the method doesn't affect the caller's variable, because Java passes primitives by value. - No overflow. The largest input,
1000000000, has a digit sum of 1, and no input can have a sum anywhere near the limit of anint.
Another way: through text
A number can also be turned into a string with String.valueOf(n) and the characters added one by one. A digit character is not its number: (int) '7' is 55, so the code subtracts '0', and '7' - '0' is 7. Summing 472 this way gives 13 as well. The remainder-and-divide loop needs no conversion and is the standard way to work with the digits of a number, which later lessons rely on.
A second trace
For n = 1005 the loop runs four times. After the passes, total and n are (5, 100), (5, 10), (5, 1) and (6, 0). The two zeros pass through n % 10 without changing total, and the loop goes on because n itself is still greater than 0. The answer is 6.
Edge cases and common mistakes
- Zero. The loop doesn't run, and
totalstays 0. - Zeros inside the number.
1005passes through 5, 0, 0 and 1. Adding a 0 is harmless, and the loop condition looks atn, not at the digits, so it doesn't stop early at a zero digit. - Forgetting
n = n / 10. This causes an infinite loop. - Returning inside the loop. The method would stop after the first digit.
Testing
472gives13.1005gives6.7gives7.0gives0.1000000000gives1.
