Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATH BASICS
Read the digits of 12040 from right to left: 0, 4, 0, 2, 1. Writing those digits in that order gives 04021, but the first 0 is a leading zero. An integer does not store that leading zero, so the reversed integer is 4021.
The zero is still part of the reversal process. It becomes the first digit you extract and later becomes the ones digit of 40. It disappears only from the written form because it is at the front of 04021, where an integer does not retain it.
Which integer represents the reversed digits of 12040?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start with reversed = 0. To append a digit, first multiply the current result by 10, which shifts every stored digit one decimal place left, then add the new digit in the empty ones position. The update is reversed = reversed * 10 + digit.
// extracted digits: 0, 4, 0, 2, 1
reversed = 0 * 10 + 0; // 0
reversed = 0 * 10 + 4; // 4
reversed = 4 * 10 + 0; // 40
reversed = 40 * 10 + 2; // 402
reversed = 402 * 10 + 1; // 4021If you write reversed = reversed + digit instead, the existing digits never move left. For the same extracted digits, the states become 0, 4, 4, 6, 7. That operation adds values, but it does not construct new decimal positions, so it cannot reverse the number.
The last digit of the current n is digit = n % 10. After using that digit, integer division by 10 removes it from n. For 12040, the first remainder is 0, and n becomes 1204. The next remainder is 4, and n becomes 120. Each division exposes the next original digit at the right edge.
| ITERATION | N BEFORE | DIGIT = N % 10 | REVERSED AFTER UPDATE | N AFTER N / 10 |
|---|---|---|---|---|
| 1 | 12040 | 0 | 0 | 1204 |
| 2 | 1204 | 4 | 4 | 120 |
| 3 | 120 | 0 | 40 | 12 |
| 4 | 12 | 2 | 402 | 1 |
| 5 | 1 | 1 | 4021 | 0 |
The loop stops when n becomes 0. At that point, every one of the five original digits has been extracted once, and the result contains them in reverse positional order. No extra pass is needed because dividing the final n value of 1 by 10 consumes its last digit and produces 0.
The loop state is n = 120 and reversed = 4. Since digit = n % 10 is 0, what is the next value of reversed?
digit = n % 10;
reversed = reversed * 10 + digit;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Initialize reversed to 0. While n is not 0, extract its last digit, append that digit with reversed = reversed * 10 + digit, and remove the consumed digit with integer division by 10. For n = 12040, the loop runs five times and returns 4021.
int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n = n / 10;
}
return reversed;
}If n has d digits, the loop runs once for each digit, so the time complexity is O(d). The algorithm stores only n, reversed, and digit, so it uses O(1) extra space. For 12040, d is 5, and the returned integer is 4021.