DSA SheetLesson · no judge

DSA FUNDAMENTALSMATH BASICS

Find the Sum of Digits by Removing One Digit at a Time

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 13

Remainder by 10 exposes the last digit

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.

CPPExtract the last digit and add it before changing working.
int working = 5832;
int sum = 0;

int digit = working % 10;  // digit is 2
sum += digit;              // sum is 2

At 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.

CHECKPOINT 1Not answered

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.

Integer division by 10 removes exactly the digit you just used

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 BEFOREEXTRACTED DIGITWORKING AFTER INTEGER DIVISION
58322583
583358
5885
550
The extraction and removal order for 5832

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 carries a growing sum and a shrinking working number

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.

CPPThe current digit is added before working is shortened.
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.

the complete digit-sum trace for n = 5832A left-to-right trace starts with working = 5832 and sum = 0. The first iteration extracts 2, changes sum to 2, and changes working to 583. The second extracts 3, changes sum to 5, and changes working to 58. The third extracts 8, changes sum to 13, and changes working to 5. The fourth extracts 5, changes sum to 18, and changes working to 0. The condition working > 0 is then false, so the result is 18.STARTworking = 5832sum = 0ITERATION 1working = 583digit = 2sum = 2ITERATION 2working = 58digit = 3sum = 5ITERATION 3working = 5digit = 8sum = 13ITERATION 4working = 0digit = 5sum = 18STOPworking > 0: falsereturn sum = 18extract 8Digit-sum trace for n = 5832
Every removed digit contributes once, so 5 + 8 + 3 + 2 = 18.
CHECKPOINT 2Not answered

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 process ends only after every digit has contributed once

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.