Java Program to Check Palindrome Number

Program to Check Palindrome 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.

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.

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: 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: 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

Leave a Comment