Java program to check if a number is palindrome or not

Let’s learn java program to check if a number is palindrome or not.

Java program to check if a number is palindrome or not

Palindrome number java is one of the common java interview question. Palindrome number is a number where number remain the same when its numbers are reversed.

For example : 141, 101, 181, 12321 etc..

Here first user enters a number. This number is stored in temporary variable. Then number is reversed. Next compare temporary number with reversed number.

Also read – linear search in java

If both temporary number and reversed number are same then it is a palindrome number else it’s not a palindrome number.

Now let’s see java program on palindrome number in java,

import java.util.Scanner;

public class PalindromeNumberInJava
{
   public static void main(String[] args) 
   { 
      System.out.println("Please enter a number to reverse : ");
      Scanner sc = new Scanner(System.in);
      int reverse, total = 0, temp;
      int number = sc.nextInt();
      temp = number; 
      while(number > 0)
      { 
         reverse = number % 10; 
         total = (total * 10) + reverse; 
         number = number / 10;
      } 
      if(temp == total)
      {
         System.out.println(total + " is a Palindrome Number");
      }
      else
      {
         System.out.println(total + " not a palindrome number");
      }
      sc.close();
   }
}



Output:

java program to check if a number is palindrome or not