Java Palindrome Number

  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. */

0 comments: