Java Palindrome Number Example

  1. /*
  2. Java Palindrome Number Example
  3. This Java Palindrome Number Example shows how to find if the given
  4. number is palindrome number or not.
  5. */
  6.  
  7.  
  8. public class JavaPalindromeNumberExample {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. //array of numbers to be checked
  13. int numbers[] = new int[]{121,13,34,11,22,54};
  14.  
  15. //iterate through the numbers
  16. for(int i=0; i < numbers.length; i++){
  17.  
  18. int number = numbers[i];
  19. int reversedNumber = 0;
  20. int temp=0;
  21.  
  22. /*
  23. * If the number is equal to it's reversed number, then
  24. * the given number is a palindrome number.
  25. *
  26. * For example, 121 is a palindrome number while 12 is not.
  27. */
  28.  
  29. //reverse the number
  30. while(number > 0){
  31. temp = number % 10;
  32. number = number / 10;
  33. reversedNumber = reversedNumber * 10 + temp;
  34. }
  35.  
  36. if(numbers[i] == reversedNumber)
  37. System.out.println(numbers[i] + " is a palindrome number");
  38. else
  39. System.out.println(numbers[i] + " is not a palindrome number");
  40. }
  41.  
  42. }
  43. }
  44.  
  45. /*
  46. Output of Java Palindrome Number Example would be
  47. 121 is a palindrome number
  48. 13 is not a palindrome number
  49. 34 is not a palindrome number
  50. 11 is a palindrome number
  51. 22 is a palindrome number
  52. 54 is not a palindrome number
  53. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!