Sunday, 1 November 2015

Palindrome Number in Java


Palindrome Number:- If a number is equal to its reversed number,then the given number is Palindrome number.
For example, 121 is a palindrome number.

Input:- { 1221, 23, 343, 112, 222, 524 }

Ouput:- 1221 is a palindrome
             23 not a palindrome
             343 is a palindrome
             112 not a palindrome
             222 is a palindrome
             524 not a palindrome


Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class PalindromeExample {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // array of numbers to be checked  
           int numbers[] = new int[] { 1221, 23, 343, 112, 222, 524 };  
           // iterate through the numbers  
           for (int i = 0; i < numbers.length; i++) {  
                int number = numbers[i];  
                int reversedNumber = 0;  
                int temp = 0;  
                // reverse the number  
                while (number > 0) {  
                     temp = number % 10;  
                     number = number / 10;  
                     reversedNumber = reversedNumber * 10 + temp;  
                }  
                if (numbers[i] == reversedNumber)  
                     System.out.println(numbers[i] + " is a palindrome");  
                else  
                     System.out.println(numbers[i] + " not a palindrome ");  
           }  
      }  
 }  


Output  
 1221 is a palindrome  
 23 not a palindrome   
 343 is a palindrome  
 112 not a palindrome   
 222 is a palindrome  
 524 not a palindrome  



Enjoy Programming :)

No comments:

Post a Comment