tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Core > Special Numbers > Palindrome Number

Palindrome Number 

A palindrome number is a number which produces the same number when the digits in the number are arranged in reverse order.

File Name  :  
com/bethecoder/tutorials/core/special_numbers/Palindrome.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.core.special_numbers;

public class Palindrome {

  public static void main (String [] args) {

    isPalindromeSimple(234);
    isPalindromeSimple(232);
    isPalindromeSimple(2332);
    isPalindromeSimple(23232);
    isPalindromeSimple(12345);
    
    System.out.println();
    
    isPalindromeComplex(234);
    isPalindromeComplex(232);
    isPalindromeComplex(2332);
    isPalindromeComplex(23232);
    isPalindromeComplex(12345);
  }
  
  private static boolean isPalindromeSimple(int number) {
    String num = String.valueOf(number);
    boolean palindrom = new StringBuilder(num).reverse().toString().equals(num);
    System.out.println(number + " is " 
        (palindrom ? "a Palindrome" "not a Palindrome"));
    return palindrom;
  }
  
  private static boolean isPalindromeComplex(int number) {
    int revNum = 0;
    int numTemp = number;
    
    while (numTemp > 0) {
      revNum = revNum * 10 (numTemp % 10);
      numTemp = numTemp / 10;
    }
    
    boolean palindrom = (revNum == number);
    System.out.println(number + " is " 
        (palindrom ? "a palindrom" "not a Palindrome"));
    return palindrom;
  }
  
}
   

It gives the following output,
234 is not a Palindrome
232 is a Palindrome
2332 is a Palindrome
23232 is a Palindrome
12345 is not a Palindrome

234 is not a Palindrome
232 is a palindrom
2332 is a palindrom
23232 is a palindrom
12345 is not a Palindrome


                    Print

   



Books Recommendation

 
 
bl  br