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 reversed.

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

Also read – armstrong number in java

In the below example first user enters a number using nextInt() method of Scanner class. This number is stored in ‘number’ variable.

This ‘number’ variable value is stored in ‘givenNumber’ variable.

Because at the end we have to compare values of ‘reversedNumber’ variable and ‘givenNumber’ variable.

Then in while loop ‘number’ variable is looped until it is equal to zero.

In each iteration of while loop last digit of ‘number’ variable is stored in ‘remainder’ variable.

Then remainder is added to reversedNumber in such a way that it is added to next place value.

In the next step last digit is removed from ‘number’ variable after dividing by 10.

Finally ‘givenNumber’ and ‘reversedNumber’ is compared.

If it is equal it a palindrome number else it is 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)
   {
      int remainder, reversedNumber = 0, givenNumber;
      System.out.println("Please enter a number: ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      givenNumber = number;
      while(number != 0)
      {
         remainder = number % 10;
         reversedNumber = reversedNumber * 10 + remainder;
         number /= 10;
      }
      if(givenNumber == reversedNumber)
      {
         System.out.println(givenNumber + " is a palindrome number.");
      }
      else
      {
         System.out.println(givenNumber + " 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.