Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATH BASICS
The number 12021 is a palindrome because reading its digits from left to right gives the same value as reading them from right to left. Apply digit reversal to every digit, not just the first or last few digits. Reversing 12021 produces 12021, so the exact test is 12021 == 12021. A matching digit or a partial reversed value cannot establish the result, because the remaining digits may still differ.
The comparison must use two complete values: one value represents the number before any digits are removed, and the other represents the number built by reversal. If either value is only a partial result, the test is not checking whether the entire number reads the same in both directions.
For n = 12021, which two complete values must be compared to check for a palindrome?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The digit-reversal loop consumes its input through working, so copy n before the loop changes anything. Give each variable one job: original keeps the starting value, working is reduced one digit at a time, and reversed receives those digits in reverse order.
int n = 12021;
int original = n;
int working = n;
int reversed = 0;
while (working > 0) {
int digit = working % 10;
reversed = reversed * 10 + digit;
working = working / 10;
}
// Compare reversed with original hereAfter the loop, working no longer contains the starting number. It has been reduced to 0 because every digit was removed. original is the only variable that still holds 12021 without being changed, so it is the value that must be used for the final comparison.
Each iteration removes the last digit from working and appends that digit to reversed. For n = 12021, working shrinks from 12021 to 0, while reversed grows from 0 to 12021. The zero digit appears on the third iteration. Adding it still matters: reversed becomes 120 * 10 + 0, which is 1200, but in this trace the value before that step is 12, so the actual result is 120. The zero is preserved in its place rather than being skipped.
| ITERATION | WORKING BEFORE | DIGIT REMOVED | REVERSED AFTER | WORKING AFTER |
|---|---|---|---|---|
| 1 | 12021 | 1 | 1 | 1202 |
| 2 | 1202 | 2 | 12 | 120 |
| 3 | 120 | 0 | 120 | 12 |
| 4 | 12 | 2 | 1202 | 1 |
| 5 | 1 | 1 | 12021 | 0 |
The third iteration shows why the multiplication by 10 is essential. Before it, reversed is 12. Multiplying by 10 shifts those digits left to make room for the next digit, and adding 0 keeps the new value at 120. The zero occupies the middle position of the final reversal.
Replace the incorrect final condition so the palindrome test uses the untouched value.
if (reversed == working) {
// palindrome
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Once the loop finishes, reversed is 12021 and original is still 12021. Therefore reversed == original is true, and 12021 is a palindrome. The alternative comparison reversed == working checks 12021 == 0, which is false because working records that all digits have been consumed, not the number that was originally given.
The process examines each of the five digits exactly once. It uses a fixed number of variables, regardless of how many digits the number has: original stores the input, working drives the loop, and reversed stores the result. The correctness of the final test depends on preserving original until the reversal is complete.