DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSJAVA FUNDAMENTALS

Standard Practice Problems in Java: Process Every Digit Safely

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

Remainder and division can remove one decimal digit at a time

For temp = 1221, the expression temp % 10 selects the rightmost digit, while temp / 10 removes that digit through integer division. Repeating both operations processes the number from right to left. The digits appear as 1, 2, 2, and 1, then temp becomes 0 and the loop stops.

JAVAEach pass reads the last digit, then removes it from temp.
int temp = 1221;

while (temp > 0) {
    int digit = temp % 10;
    temp = temp / 10;
}
CURRENT TEMPDIGIT = TEMP % 10NEXT TEMP = TEMP / 10
12211122
122212
1221
110
The value of temp after each digit is removed
CHECKPOINT 1Not answered

Given temp = 1221, which pair produces digit = 1 and next temp = 122?

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

A working copy can reach 0 without destroying the original input

Store 1221 in original, then copy it into temp. The loop changes temp from 1221 to 122, then 12, then 1, and finally 0. The value in original never changes, so it remains available after every digit has been processed.

JAVAThe loop consumes temp, not original.
int original = 1221;
int temp = original;

while (temp > 0) {
    int digit = temp % 10;
    temp = temp / 10;
}

// original is still 1221, while temp is 0

If you divide original directly inside the loop, original eventually becomes 0. That is useful for stopping the loop, but it leaves no copy of 1221 for a later comparison. A palindrome test that needs the original value must preserve it before the loop starts.

the digit-by-digit reduction of temp for original = 1221A fixed box at the side contains original = 1221. A left-to-right trace shows temp = 1221 producing digit 1 and becoming 122, temp = 122 producing digit 2 and becoming 12, temp = 12 producing digit 2 and becoming 1, and temp = 1 producing digit 1 and becoming 0. The trace ends because temp is 0, while the original box still contains 1221.original = 1221Iteration 1temp = 1221 digit = 1next temp = 122Iteration 2temp = 122 digit = 2next temp = 12Iteration 3temp = 12 digit = 2next temp = 1Iteration 4temp = 1 digit = 1next temp = 0Loop stoptemp = 0 → condition falseoriginal = 1221 (still available)
The working copy is consumed, but the original value survives.

The accumulator update decides which digit problem the loop solves

The same extracted digit can update several accumulators. Add 1 to count for every pass, add digit to sum, and place digit into reversed. The loop does not need a new structure for each task. Only the update applied after extraction changes.

JAVAOne digit-processing loop maintains three different results.
int count = 0;
int sum = 0;
int reversed = 0;
int temp = 1221;

while (temp > 0) {
    int digit = temp % 10;
    count = count + 1;
    sum = sum + digit;
    reversed = reversed * 10 + digit;
    temp = temp / 10;
}
DIGITCOUNTSUMREVERSED
1111
22312
235122
1461221
Accumulator values after each digit of 1221 is extracted
CHECKPOINT 2Not answered

Complete the reverse update for the digits of 1221.

reversed = reversed * 10 + ____;

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

A palindrome decision is valid only after the complete reverse is built

After the loop finishes, temp is 0 but reversed contains the complete number in reverse order. Compare reversed with original only then. For 1221, both values are 1221, so the number is a palindrome.

JAVAThe comparison uses the preserved original after the reverse is complete.
int original = 1221;
int temp = original;
int reversed = 0;

while (temp > 0) {
    int digit = temp % 10;
    reversed = reversed * 10 + digit;
    temp = temp / 10;
}

if (reversed == original) {
    System.out.println("1221 is a palindrome");
} else {
    System.out.println("1221 is not a palindrome");
}

Comparing reversed with the consumed value of original is a different operation: after direct division, that value is 0. The comparison then rejects 1221 even though its reverse is 1221. Matching the first and last digits alone is also not enough, because the middle digits still need to be processed before the decision is complete.

Previous · Do-While LoopNext part · Quiz