Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
For 507204, the rightmost digit is 4. The remainder operator exposes it: 507204 % 10 gives 4. Integer division removes that rightmost digit: 507204 / 10 gives 50720. These operations inspect the same working number, but they produce different parts of the next state.
int working = 507204;
int digit = working % 10; // 4
working = working / 10; // 50720A while loop can repeat this pair of operations while the working number is not zero. Each iteration processes one digit from right to left. The zero digits in 507204 are still processed: a remainder of 0 is a real digit, not a signal to skip the iteration.
After one iteration starts with working number 507204, what digit is extracted and what working number remains?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start with answer = 0. After extracting 4, the answer should be 4. When the next digit is 0, the old answer 4 must become 40 before that zero is added. The update answer = answer * 10 + digit performs both actions: multiplication creates an empty ones place, and addition fills it with the new digit.
int answer = 0;
answer = answer * 10 + 4; // 4
answer = answer * 10 + 0; // 40
answer = answer * 10 + 2; // 402
answer = answer * 10 + 7; // 4027
answer = answer * 10 + 0; // 40270
answer = answer * 10 + 5; // 402705The tempting update answer = answer + digit does not preserve decimal positions. After the first two digits, it gives 4 + 0 = 4 instead of 40, so the zero disappears. Continuing with simple addition gives 4 + 0 + 2 + 7 + 0 + 5 = 18, not 402705. The answer's digits do not shift left automatically.
For a positive working number, integer division by 10 removes one decimal digit on every iteration. Starting at 507204, six iterations change the working number to 50720, 5072, 507, 50, 5, and finally 0. Once the working number is 0, the loop condition is false and no seventh digit is processed.
| ITERATION | WORKING NUMBER BEFORE | EXTRACTED DIGIT | ANSWER AFTER | WORKING NUMBER AFTER |
|---|---|---|---|---|
| 1 | 507204 | 4 | 4 | 50720 |
| 2 | 50720 | 0 | 40 | 5072 |
| 3 | 5072 | 2 | 402 | 507 |
| 4 | 507 | 7 | 4027 | 50 |
| 5 | 50 | 0 | 40270 | 5 |
| 6 | 5 | 5 | 402705 | 0 |
Division by 10 must happen on every iteration, including the iterations that extract 0. If the working number is 50720 and the code extracts 0 but does not divide, the working number stays 50720. The loop condition stays true, so the same zero is extracted forever and the loop cannot reach termination.
Replace the incorrect update so that answer changes from 4027 to 40270 when the next extracted digit is 0.
answer = answer + digit;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Within one iteration, extract the digit before dividing the working number, then update the answer before moving to the next iteration. With 507204, extraction first gives 4, so that 4 can be appended. Dividing first changes the working number to 50720, and the original 4 is gone. No later operation can recover a digit that integer division already removed.
int working = 507204;
int answer = 0;
while (working != 0) {
int digit = working % 10;
answer = answer * 10 + digit;
working = working / 10;
}
// answer is 402705, working is 0The final update in each iteration also matters. After the sixth iteration, the answer is 402705 and the working number becomes 0. The next condition check stops the loop. The algorithm therefore preserves every digit, including both zeros, while using the shrinking working number to determine when all six digits have been processed.