Palindrome Program in Java

A string is called a Palindrome if it is spelled the same forward and backward. Following are some examples of palindrome strings,

Madam, Noon, Rotor, Rotator, Radar, Malayalam

Checking whether a string is palindrome or not is a common programming assignment given to programmers learning a new language.

Checking Whether a String is a Palindrome in Java

The following Java program checks whether a given string is a palindrome or not. There are various ways to program this. In this example we will first reverse the string and then check whether the string is equal to the original string.

public class PalindromeChecker {

    public static void main(String[] args) {
        String string_to_check="rotator";
        if(isPalindrome(string_to_check)) {
            System.out.println(string_to_check+" is a palindrome");
        }else {
            System.out.println(string_to_check+" is NOT a palindrome");
        }
    }
    
    public static boolean isPalindrome(String s) {
        
        String reverse="";
        for(int i=s.length()-1;i>=0;i--) {
            reverse = reverse+s.charAt(i);
        }
        if(s.equalsIgnoreCase(reverse)) {  // case insensitive check
            return true;
        }else {
            return false;
        }
    }
    
}

A number is called a Palindrome number if the number remains the same when digits are reversed.

Checking Whether a Number is a Palindrome in Java

The following Java program checks whether a given number is a palindrome or not. In this example we first convert the number to a string. Then we check whether the reversed string is same as the original.

public class PalindromeNumberChecker {

    public static void main(String[] args) {
        int number_to_check=1001;
        String s = String.valueOf(number_to_check);
        if(isPalindrome(s)) {
            System.out.println(number_to_check+" is a palindrome number");
        }else {
            System.out.println(number_to_check+" is NOT a palindrome number");
        }
    }
    
    public static boolean isPalindrome(String s) {
        
        String reverse="";
        for(int i=s.length()-1;i>=0;i--) {
            reverse = reverse+s.charAt(i);
        }
        if(s.equalsIgnoreCase(reverse)) { 
            return true;
        }else {
            return false;
        }
    }
 
}

Another way to check for palindrome numbers without converting them to strings is given below. In this example, we convert the number to the reverse number and then check whether they are same.

public class PalindromeNumberChecker2 {

    public static void main(String[] args) {
        int number_to_check=882;
        if(isPalindrome(number_to_check)) {
            System.out.println(number_to_check+" is a palindrome number");
        }else {
            System.out.println(number_to_check+" is NOT a palindrome number");
        }
    }
    
    public static boolean isPalindrome(int n) {
        
        int reverse_number = 0;
        int original_number = n;
        while(n > 0) {
            int digit = n % 10;
            reverse_number = reverse_number * 10 + digit;
            n = n / 10;
        }
        if(original_number == reverse_number) {
            return true;
        }else {
            return false;
        }
    }
    
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution on this site within 48 hours! Please email us.


Leave a Reply