DSA FUNDAMENTALS › MATH BASICS
Start with working = 5832 and sum = 0. The expression working % 10 gives the digit in the units place, so 5832 % 10 produces 2. That 2 is the first digit you need to add to sum. The number does not become shorter until after this digit has been saved in the sum.
int working = 5832;
int sum = 0;
int digit = working % 10; // digit is 2
sum += digit; // sum is 2At this point, the state is digit = 2, sum = 2, and working = 5832. Extracting a digit only reads it. Adding the digit changes sum, but it still leaves working unchanged. You need a separate removal step to shorten working after the digit has contributed.
What is the state immediately after the first digit of 5832 is extracted and added, but before that digit is removed?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After 2 has been added, integer division by 10 removes the last digit from the working copy: 5832 / 10 becomes 583. The next remainder is 583 % 10, which is 3. Dividing again gives 58, then 5, then 0. Each division must follow the extraction and addition for the same state.
| WORKING BEFORE | EXTRACTED DIGIT | WORKING AFTER INTEGER DIVISION |
|---|---|---|
| 5832 | 2 | 583 |
| 583 | 3 | 58 |
| 58 | 8 | 5 |
| 5 | 5 | 0 |
The pair for the first step is working = 5832 and digit = 2, followed by working = 583. The pair for the second step is working = 583 and digit = 3, followed by working = 58. This same order continues until division produces 0. If you divide first, the current last digit is no longer available to extract.
The loop repeats while working is greater than 0. During every iteration, it extracts the current last digit, adds that digit to sum, and then divides working by 10. The loop invariant is that sum contains the total of every digit already removed from working. At the start, no digits have been removed and sum is 0.
int working = 5832;
int sum = 0;
while (working > 0) {
int digit = working % 10;
sum += digit;
working /= 10;
}In the first iteration, digit = 2, sum becomes 2, and working becomes 583. In the second, digit = 3, sum becomes 5, and working becomes 58. In the third, digit = 8, sum becomes 13, and working becomes 5. In the fourth, digit = 5, sum becomes 18, and working becomes 0.
Complete the missing update so 5832 reaches sum = 18. Add working % 10 to sum before dividing working by 10.
while (working > 0) {
sum += working % 10;
// missing update
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The condition working > 0 becomes false when the fourth division changes working from 5 to 0. By then, the extracted digits were 2, 3, 8, and 5, and sum is 18. The final zero is a stopping state, not another digit to add. Each positive working value gives one iteration, so every digit in 5832 contributes exactly once before the loop ends.
If a number has d digits, integer division by 10 removes one digit per iteration, so the loop takes O(d) time. It keeps only working, sum, and the current digit, so it uses O(1) extra space. The essential correctness condition is unchanged: extraction and addition must happen before removal.