Loading...
Loading...
Loading...
A cinema gives free popcorn when a ticket number reads the same forwards and backwards, like 1221 or 7. For each ticket, print YES if it is a palindrome and NO otherwise.
Write static long reverse(long n) and static boolean isPalindrome(long n), where isPalindrome calls reverse. Work only with numbers: no Strings.
YES or NO.Example
Input
3 1221 1234 7
Output
YES NO YES
1221 and 7 read the same both ways; 1234 does not.
Try the problem first. Open one hint at a time.
A number is a palindrome when it equals its own reverse.
Reuse the digit-reversing loop: rev = rev * 10 + n % 10; n /= 10;.
Keep the original n for the comparison; the reversing happens on the method's own copy.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor