Determines whether an integer is a return number. Check if a number is palindrome

Source: Internet
Author: User

One way is to flip the current number first, and then compare it with the original number (omitted)

Another method is recursion. It uses a copy number to recursion the original number to flip it, And then compares it with the copy number.


Package recursion; public class Check_if_a_number_is_palindrome {public static void main (String [] args) {int num = 121321; System. out. println (check (num); num = 12321; System. out. println (check (num); num = 123321; System. out. println (check (num);} // One method is to flip the current number first, and then compare it with the original number (omitted) // The other method is the recursive method static int dupNum; public static boolean check (int num) {dupNum = num; // copy the original number return rec (num );} public static boolean rec (in T num) {if (isSingleDigit (num) {return num = dupNum % 10;} // recursively causes num: 121-> 12-> 1 is equivalent to comparing if (! Rec (num/10) {// if the previous comparison has failed, you do not need to continue to compare return false;} else {dupNum/= 10; return num % 10 = dupNum % 10; // compare the last digit of the original number with the last digit of the recursive number} // helper function: determines whether it is a single-digit public static boolean isSingleDigit (int num) {if (num >=0 & num <10) {return true;} else {return false ;}}}



Http://www.geeksforgeeks.org/check-if-a-number-is-palindrome/






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.