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

Check if a number is palindrome or not 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..

Also read – armstrong number in java

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

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 to check if a number is palindrome or not.

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

Also read – String palindrome in java using array

Also read – prime number program in java

Also read – palindrome string program in java

Also read – string palindrome in java using reverse method


Without using reverse method

Let’s learn string palindrome in java without using reverse method. In java program to check if any string is palindrome without using reverse method first convert user entered string to char array.

Also read – factorial program in java

In the next step read char from char array from last index. Lastly concatenate. Let’s see java program.

import java.util.Scanner;
public class StringPalindromeDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to check palindrome: ");
      String strInput = sc.nextLine();
      // converting string to char array
      char[] chString = strInput.toCharArray();       
      // storing reverse string
      String strReverse = "";         
      // reading char by char
      for(int a = chString.length - 1; a >= 0; a--) 
      {
         strReverse = strReverse + chString[a];
      } 
      // printing given string and reversed string
      System.out.println("Given string: " + strInput);
      System.out.println("Reverse String: " + strReverse); 
      // check if given string is palindrome
      if(strInput.equals(strReverse))
      {
         System.out.println("string is palindrome.");
      }
      else
      {
         System.out.println("string is not palindrome.");
      }
   }
}


Output:

Please enter string to check palindrome: madam
Given string: madam
Reverse String: madam
string is palindrome.

Please enter string to check palindrome: abcd
Given string: abcd
Reverse String: dcba
string is not palindrome.


palindrome number using recursion in java

Here’s the program to check if number is palindrome or not using recursion.

public class PalindromeNumberRecursion
{
   static int palindromeRecursion(int number, int temp)
   {
      if(number == 0)
         return temp;
      // storing reverse of a number
      temp = (temp * 10) + (number % 10);
      return palindromeRecursion(number / 10, temp);
   }
   public static void main(String[] args)
   {
      int number = 141;
      int temp = palindromeRecursion(number, 0);
      if(temp == number)
      {
         System.out.println("number is palindrome.");
      }
      else
      {
         System.out.println("number is not palindrome.");
      }
   }
}


Output:

number is palindrome.