codescracker


java

Java Program to Check Palindrome or Not



« Previous Program Next Program »


Check Palindrome or Not

To check whether the input number is palindrome or not in Java programming, first reverse the number and check whether the reverse is equal to original or not. If it is equal then the number is palindrome, otherwise the number is not a palindrome number

Java Programming Code to Check Palindrome or Not

Following Java Program ask to the user to enter a number to reverse it, then check whether the reverse is equal to its original or not, if it is equal then it will be palindrome, otherwise it will not be a palindrome number:

/* Java Program Example - Check Palindrome or Not */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int num, rem, orig, rev=0;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        orig = num;
		
        while(num != 0)
        {
            rem = num%10;
            rev = rev*10 + rem;
            num = num/10;
        }
        
        // check if the original number is equal to its reverse
        
        if(rev==orig)
        {
            System.out.print("This is a Palindrome Number");
        }
        else
        {
            System.out.print("This is not a Palindrome Number");
        }
    }
}

When the above Java Program is compile and executed, it will produce the following result. Above Java Programming Example Output (palindrome):

Java Program check palindrome

Above Java Programming Example Output (not palindrome):

check palindrome in java

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test