Java program to check number is palindrome or not

 

package testing;
import java.util.Scanner;
/**
 * Java program to check if number is palindrome or not.
 * A number is called palindrome if number and its reverse is equal
 * This Java program can also be used to reverse a number in Java
 */
public class NoClassDefFoundErrorDueToStaticInitFailure {
    public static void main(String args[]){
       
        System.out.println("Please Enter a number : ");
        int palindrome = new Scanner(System.in).nextInt();
       
        if(isPalindrome(palindrome)){
            System.out.println("Number : " + palindrome + " is a palindrome");
        }else{
            System.out.println("Number : " + palindrome + " is not a palindrome");
        }      
       
    }
    /*
     * Java method to check if number is palindrome or not
     */
    public static boolean isPalindrome(int number) {
        int palindrome = number; // copied number into variable
        int reverse = 0;
        while (palindrome != 0) {
            int remainder = palindrome % 10;
            reverse = reverse * 10 + remainder;
            palindrome = palindrome / 10;
        }
        // if original and reverse of number is equal means
        // number is palindrome in Java
        if (number == reverse) {
            return true;
        }
        return false;
    }
}

Output:
Please Enter a number :
123
Number : 123 is not a palindrome
Please Enter a number :
121
Number : 123 is a palindrome

Share Your Comments
Previous Some Blogger Template Errors and How to Fix Them
Next Java program to find factorial of a number

About author

Aamir Shaikh
Aamir Shaikh 1370 posts

A Blogger, SEO Analyst and passionate Web Developer from India. Believes in hard work and continues working to achieve success.

View all posts by this author →

You might also like