How to Find If A Number is A Palindrome in Java?

In java, sequence of digits in a number can be reversed by performing division and modulo operations on the given number. Below is the code snippet in Java to achieve the same:

Palindrome in Java

public class ReverseNumber {


  public static void main(String[] args) {

    int num = 12345;

    int origNum = num;

    int remainder;

    int revNum = 0;

    while (num > 0) {

      remainder = num % 10;

      revNum = revNum *10 + remainder;

      num = num / 10;


    System.out.println(“revNum: ” + revNum);



Output of this program would be:
revNum: 54321

The same code snippet can be used to find out if the given number is a palindrome.

To check that, below code snippet needs to be added after the “System.out.println” statement.

if (origNum == revNum) {

  System.out.println(“Number is a palindrome”);

} else {

  System.out.println(“Number is not a palindrome”);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s