Java Program to check Palindrome Number or Not

205
views
Java Program to check Palindrome Number or Not

In this program, you’ll learn how to check Palindrome Number or Not in java.

To properly understand this Program to check Palindrome number, you should have the knowledge of following Java programming topics:

  • Java Variables
  • Java loops
  • Basic operator
  • Java Control Statements

First of all, let us understand what palindrome number is.

A palindrome number or numeral palindrome is a number that remains the same when its digits are reversed.

For example, consider the number 16461, it remains the same when the digits are reversed.

Program to check for palindrome number

public class Palindrome{
    public static void main(String[] main){
        int number=16461;
        int temp,remainder,sum=0;
        temp=number;
        while(number!=0){
            remainder=number%10;
            sum=(sum*10)+remainder;
            number=number/10;
        }
        if(temp==sum){
            System.out.println("Number is Palindrome");
        }
        else{
            System.out.println("Number is not Palindrome");
        }
    }
}

Output

Number is Palindrome

In the above program to check for Palindrome Number There are three steps which are as follow:

  1. First, we store the number to check is in temp variable, because at last, we need to compare the number with the reverse number.
  2. In the while loop, we perform the following steps:
    1. First, we get the last digit of a number by dividing it by 10 and store it in, the variable remainder.
    2. Next, we add the remainder to the last digit of the sum variable by multiplying it with 10 and then add the remainder to it.
    3. The last step is to eliminate the last digit of a number by dividing the number by 10 and store the quotient in variable number.
  3. In the final step we compare the variables temp and sum, if they are equal that means the number is palindrome otherwise it is not a palindrome number.

Ask your questions about check Palindrome number in Java and clarify your/others doubts by commenting. Documentation.

Related Java Programs

Please write to us at [email protected] to report any issue with the above content or for feedback.

This Program is contributed by Ashutosh Sahu.