How to check if a number is a palindrome or not in Java - Example

How to check if a number is a palindrome or not is a variant of  popular String interview question how to check if a String is a palindrome or not. A number is said to be a palindrome if number itself is equal to reverse of number e.g. 313 is a palindrome because reverse of this number is also 313. On the other hand 123 is not a palindrome because reverse of 123 is 321 which is not equal to 123, i.e. original number. In order to check if a number is a palindrome or not we can reuse the logic of How to reverse number in Java. Since in most of interview, you are supposed to solve this question without taking help from API i.e. only using basic programming construct e.g. loop, conditional statement, variables, operators and logic. I have also seen programmer solving this question by first converting integer to String and than reversing String using reverse() method of StringBuffer and than converting String back to Integer, which is not a correct way because you are using Java API. Some programmer may think that this is just a trivial programming exercise but it’s not. Questions like this or Fibonacci series using recursion can easily separate programmers who can code and who can’t. So it’s always in best interest to keep doing programing exercise and developing logic.
Read more »

1 Response to "How to check if a number is a palindrome or not in Java - Example"

  1. Nice blog with best explanation on palindrome number in java. Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/palindrome-number-in-java/

    BalasHapus