Easy+ ยท digit building
Reverse a Number
12345 โ 54321. Peel digits off the back and build them onto a new number โ the most-asked interview warm-up there is.
Problem
Input: one integer n (0 โค n โค 10โน).
Output: the digits of n in reverse order (as an integer โ leading zeros disappear).
Sample:
| Input | Output |
|---|---|
12345 | 54321 |
1000 | 1 |
Your solution
Custom input for "Run my input"
Judging runs entirely in your browser against hidden tests. Your code is saved locally as you type. Ctrl+Enter judges.
Stuck? Hints
Hint 1 โ build, don't collect
No strings needed: rev = rev * 10 + n % 10; shifts what you have left and appends the peeled digit. Then n = n / 10;.
Hint 2 โ why 1000 โ 1
As an INTEGER, reversed "0001" is just 1 โ the math drops leading zeros automatically. A string approach would need trimming.
Solution (try first!)
int n = int.Parse(Console.ReadLine());
int rev = 0;
while (n > 0) { rev = rev * 10 + n % 10; n = n / 10; }
Console.WriteLine(rev);
Note n = 0 never enters the while โ rev stays 0, which prints correctly. Next: Palindrome Number.