Loading...
Loading...
Loading...
A child reads bus numbers from right to left for fun. Given a number n, print its digits in reverse order as a number, so trailing zeros disappear (1200 becomes 21).
Write a method static long reverse(long n) that returns the reversed number.
Example 1
Input
1234
Output
4321
The digits 1 2 3 4 read backwards.
Example 2
Input
1200
Output
21
0021 is printed as the number 21.
Try the problem first. Open one hint at a time.
Take the last digit with n % 10 and put it at the end of the answer: rev = rev * 10 + digit.
Then remove the digit with n / 10, and repeat while n > 0.
Use long for the answer: reversing a 10-digit number can go past the int limit.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor