How to check if a number is palindrome in java

A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, generally if used comma, separators or other word dividers are ignored. [Not mandatory]

Similarly, palindrome numbers are those numbers which represent same number if all digits are reversed (Underscores can be ignored in large numbers such as 1_00_00_001). Underscores are new addition in java 7 features.

To verify, if a given number is palindrome is or not, we need to reverse the number and compare with original number if both are equal or not.

Lets write some code to solve this.

package com.howtodoinjava.puzzle;

public class PalindromeTest
{
    /**
     * Test the actual code if it works correctly
     * */
    public static void main(String[] args)
    {
        System.out.println(checkIntegerPalindrome( 100 )); //false
        System.out.println(checkIntegerPalindrome( 101 )); //true
        System.out.println(checkIntegerPalindrome( 500045 )); //false
        System.out.println(checkIntegerPalindrome( 50005 )); //true
    }

    /**
     * This function will test the equality if a number and its reverse.
     * @return true if number is palindrome else false
     * */
    public static boolean checkIntegerPalindrome(int number)
    {
        boolean isPalindrome = false;
        if(number == reverse(number))
        {
            isPalindrome = true;
        }
        return isPalindrome;
    }

    /**
     * This function will reverse a given number.
     * @return reverse number
     * */
    public static int reverse(int number)
    {
        int reverse = 0;
        int remainder = 0;
        do {
            remainder = number % 10;
            reverse = reverse * 10 + remainder;
            number = number / 10;

        } while (number > 0);
        return reverse;
    }
}

Output:

false
true
false
true

If you need to test floating point numbers then only option is to convert them in string and compare the number and its reverse for string equality. Remember to remove period from strings before testing the equality. Also, test the equality using equals() method, and do not use “==” operator.

Happy Learning !!

4 thoughts on “How to check if a number is palindrome in java”

  1. class A
    {
    int v;
    A()
    {
    calc();
    }
    public void calc()
    {
    System.out.println(“A class Method “);
    }
    }
    class B extends A
    {
    B()
    {
    calc();
    }
    public void calc()
    {
    System.out.println(“B class Method “);
    }
    public static void main(String ar[])
    {
    A a1 = new B();
    }
    }

    output

    B class Method
    B class Method

    calc() method in A class is why not executed in A class Constuctor or why execute calc() method of B class
    sir plz solve my problem

    1. Because you have override the method. Any method called is always first looked upon class which instance is being created. If not found there, ONLY then super class is looked upon. In both constructor calls, calc() method is found in B so JVM does not go beyond that i.e. A is not looked into.

      1. We can easily do this in java using StringBuilder /StringBuffer also.

        StringBuilder str = new StringBuilder(“malayalam”);
        System.out.println(“reverse = ” + str.reverse());

        then compare original string and reversed one

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *


− eight = 0

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>