Find a Number Is Palindrome Or Not in Java

You are currently viewing Find a Number Is Palindrome Or Not in Java

In this article, You are going to learn the concept of “Find a Number Is Palindrome Or Not in Java”. Initially, We will discuss what is a palindrome number? and how to do a task using the java program? Let’s see the possibilities below:

What is a Palindrome?

A number that remains the same when its digits are reversed is called the palindrome number.

For example, The input number is “2112”. It is a palindrome number because the reverse of the number is also the same.

If the input number is “1257” then it will be not a palindrome number. The reverse of the number is not the same.

Find a Number Is Palindrome Or Not in Java

We can find a number is palindrome or not using java in the below program.

class Main {
  public static void main(String[] args) {
    
    int num = 19591, reversedNum = 0, remainder;
    
    // store the number to originalNum
    int originalNum = num;
    
    // get the reverse of originalNum
    // store it in variable
    while (num != 0) {
      remainder = num % 10;
      reversedNum = reversedNum * 10 + remainder;
      num /= 10;
    }
    
    // check if reversedNum and originalNum are equal
    if (originalNum == reversedNum) {
      System.out.println(originalNum + " is Palindrome.");
    }
    else {
      System.out.println(originalNum + " is not Palindrome.");
    }
  }
}

Program Explanation

The input number and the reversed number will store in the respective variable. The remainder value will calculate using the while loop. The number will be divisible by 10 and the output will be stored in the remainder variable.

After that, The reverse of an input number will be multiplied by 10 and added to the remainder value. Later, The input number will be divisible by 10.

At last, The if…condition will check whether the reversed number is equal to the original number. If it is equal then it will print “It is a palindrome number”.

Otherwise, It will print “It is not a palindrome number” using the else condition. Finally, The system.out.printIn() function will display the output on the screen.

Read Also : Find a String Is Palindrome Or Not in Java

Note

You can also Find a Number is Palindrome or Not in PHP. If you want to learn then please read the article which I have already shared on our webpage.

Output of Find a Number is Palindrome or Not in Java

The example output of ” Find a Number is Palindrome or Not in Java” will be provided below for your reference. The input number will be either a user’s choice or your wish.

19591 is Palindrome.

Conclusion

I hope this article helps you to Find a Number is Palindrome or Not in Java. If you face any issues please let me know via the comment section. Share this article with other Java developers via social networks. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply