Java Program to Check Palindrome Number

In the previous article, we have discussed Java Program to Check nth Prime Number

In this article we are going to understand what Palindrome number is and how we can check whether a number is Palindrome or not in Java with examples.

Program to Check Palindrome Number

Palindrome numbers are numbers whose reverse is the number itself.

Example :

141: Palindrome number
19: Not Palindrome number
12321: Palindrome number

In the above examples the numbers 141 and 12321 are only Palindromes. 91 is the reverse of second number 19 which is different. Hence 19 is not a Palindrome number.

Let’s see different ways to check palindrome number.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach :

  1. Enter/declare a number and store it .
  2. We reverse the digits of the number using a while loop.
  3. If the reversed number is the same as the entered number, then the number is said to be a palindrome number.

Method-1:Java Program to Check Palindrome Number By Using Static Value

import java.util.Scanner;

public class PalindromeNumber
{
    public static void main(String args[])
    {
        //A number declared
        int num = 55;

        int temp = num, remainder, reverse=0;
        //Reverses the digits of the number
        while(temp>0)
        {
            remainder = temp%10;
            reverse = reverse*10 + remainder;
            temp = temp/10;
        }

        // Checks whether the number is palindrome or not
        if(num==reverse)
        {
            System.out.println(num+" is a Palindrome Number");
        }
        else
        {
            System.out.println(num+" is Not a Palindrome Number");
        }
    }
}
Output:

55 is a Palindrome Number

Method-2:Java Program to Check Palindrome Number By User Input Value

import java.util.Scanner;
public class PalindromeNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int temp = num, remainder, reverse=0;
        //Reverses the digits of the number
        while(temp>0)
        {
            remainder = temp%10;
            reverse = reverse*10 + remainder;
            temp = temp/10;
        }

        // Checks whether the number is palindrome or not
        if(num==reverse)
        {
            System.out.println(num+" is a Palindrome Number");
        }
        else
        {
            System.out.println(num+" is Not a Palindrome Number");
        }
    }
}
Output:

Case-1

Enter a number : 55
55 is a Palindrome Number

Case-2

Enter a number : 673
673 is a Palindrome Number

Method-3: Java Program to Check Palindrome Number By Using User Defined Method

import java.util.Scanner;
public class PalindromeNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check Palindrome number or not.
        checkNumber(num);
    }

    //checkNumber() method to check palindrome number
    public static void checkNumber(int num)
    {
        int temp = num, remainder, reverse=0;
        //Reverses the digits of the number
        while(temp>0)
        {
            remainder = temp%10;
            reverse = reverse*10 + remainder;
            temp = temp/10;
        }

        // Checks whether the number is palindrome or not
        if(num==reverse)
        {
            System.out.println(num+" is a Palindrome Number");
        }
        else
        {
            System.out.println(num+" is Not a Palindrome Number");
        }
    }
}
Output: 

Case-1 

Enter a number : 55 
55 is a Palindrome Number 

Case-2 

Enter a number : 673 
673 is a Palindrome Number

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs:

Leave a Comment