DSA SheetLesson · no judge

DSA FUNDAMENTALSCONTROL FLOW

Loops Practice 2: Digit Sums Need Progress and Accumulation

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

One loop can consume 5072 one digit at a time

You can process the positive integer 5072 from right to left by repeatedly reading its rightmost digit and shortening the working value. The remainder operation n % 10 gives the current rightmost digit. Integer division n / 10 removes that digit, because the fractional part is discarded. Starting with n = 5072, the digits arrive in the order 2, 7, 0, 5.

CPPEach iteration reads the rightmost digit, then shortens the working value.
int n = 5072;
int sum = 0;

while (n > 0) {
    int digit = n % 10;
    n = n / 10;
}
BEFORE THE ITERATION, NN % 10AFTER N / 10
50722507
507750
5005
550
The working value reveals one digit at a time.
CHECKPOINT 1Not answered

Given n = 5072, what do n % 10 and n / 10 produce?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

sum = sum + digit preserves what earlier iterations found

The accumulator must include the new digit without losing the total from earlier iterations. With sum = sum + digit, the first digit changes sum from 0 to 2, the next changes it to 9, the zero leaves it at 9, and the last digit changes it to 14. Each assignment uses the old sum and adds one current digit.

CPPThe new total is the previous total plus the current digit.
int digit = n % 10;
sum = sum + digit;

The shorter statement sum = digit does something different. It assigns the current digit directly to sum, replacing the previous total. For 5072, the values become 2, then 7, then 0, then 5, so the loop finishes with 5 instead of the digit sum 14. The digits were extracted, but the earlier results were discarded.

CURRENT DIGITSUM = SUM + DIGITSUM = DIGIT
222
797
090
5145
Accumulation keeps the previous total, while replacement discards it.

The loop terminates because n loses one digit on every pass

The update n = n / 10 makes the loop condition n > 0 move toward false. The working value changes from 5072 to 507, then 50, then 5, then 0. Once n is 0, there is no digit left to process, so the loop stops.

If you omit n = n / 10, n stays equal to 5072. The condition n > 0 remains true forever, and n % 10 keeps producing 2. The accumulator may keep growing if it adds that digit, but the loop never reaches the state that permits termination.

CPPThe accumulator update preserves the result, and the working-value update guarantees progress.
while (n > 0) {
    int digit = n % 10;
    sum = sum + digit;
    n = n / 10;
}
CHECKPOINT 2Not answered

Repair this loop so it preserves every digit and eventually stops for n = 5072. Type the two replacement statements in order, separated by a newline.

while (n > 0) {
    int digit = n % 10;
    sum = digit;
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The update order makes the final sum 14

The complete loop first reads the current digit, then adds it to the accumulator, then reduces the working value. That order lets the loop use the current value of n before changing it. For 5072, the four iterations add 2, 7, 0, and 5, so the accumulator finishes at 14 exactly when n becomes 0.

CPPThe three body updates process one digit, preserve the total, and remove that digit.
int n = 5072;
int sum = 0;

while (n > 0) {
    int digit = n % 10;
    sum = sum + digit;
    n = n / 10;
}
N AT STARTDIGIT = N % 10SUM AFTER ADDITIONNEXT N = N / 10
507222507
5077950
50095
55140
Every extracted digit contributes once before the loop stops.
Previous · Loops Practice 1Next part · Quiz