Palindrome in Java – Check Number and String Palindrome

Introduction

In this article, we will learn how to check palindrome in Java.

A number which is same as its reverse is a Palindrome in Java. Meaning, palindrome number remains same if we reverse the number.

palindrome in java

Example of Palindrome Numbers : 121, 343, 23432, 191 etc.

Similarly, A string is said to be a palindrome if it is same as its reverse.

Example of Palindrome Strings : MAM , MADAM , LOL etc.

Algorithm : How to check if a number is Palindrome in Java?

  1. Take the Number.
  2. Store the Number in Temp variable.
  3. Reverse the Number.
  4. Compare the Temp Number with the Reversed Number.
  5. Check if both are same.
  6. If they are same, Number is Palindrome.

Program : How to check if a number is Palindrome in Java?

There are two ways , we can check if a Number is Palindrome. As mentioned below :

  • Using while loop.
  • Using for loop.

Example 1: Check Palindrome using while loop

public class CheckPalindrome {

    public static void main(String[] args) {

        int num = 323, reversedNum = 0, rem, OriginalNum;

        OriginalNum = num;
        
        //Loop will run until the "num" does not reaches 0

        while( num != 0 )
        {
            rem = num % 10;
            reversedNum = reversedNum * 10 + rem;
            num  /= 10;
        }

        // Printing Output
        
        if (OriginalNum == reversedNum){
            System.out.println("Number is a Palindrome");
            }
        else{
            System.out.println("Number is not a Palindrome");
            }
    }
}

As the provided number 323 is a palindrome. Below will be the Output :

Number is a Palindrome

Example 2: Check Palindrome using for loop

public class CheckPalindromeInJava {

    public static void main(String[] args) {

        int num = 324, reversedNum = 0, rem, OriginalNum;

        OriginalNum = num;
        

        for( ;num != 0; num /= 10 )
        {
            rem = num % 10;
            reversedNum = reversedNum * 10 + rem;
        }

        // Printing Output
        
        if (OriginalNum == reversedNum){
            System.out.println("Number is a Palindrome");
            }
        else{
            System.out.println("Number is not a Palindrome");
            }
    }
}

As the provided number 324 is not a palindrome. Below will be the Output :

Number is not a Palindrome

Algorithm : How to check if a String is Palindrome in Java?

Let’s take a look :

  1. Take the String.
  2. Reverse the String and store it in different variable.
  3. Compare the reversed String with the original String.
  4. Check if both are same.
  5. If they are same, String is Palindrome.

Program : How to check if a String is Palindrome in Java?

There are two ways , we can check if a String is Palindrome. As mentioned below :

  • Using loop.
  • Using Standard library methods. (reverse(), equal() etc.)

Example 1: Check String Palindrome using loop



import java.util.*;   

class CheckPalindromeInJava  
{  

   public static void main(String args[])  
   {  
   
      String str , reverse = "";
      
      Scanner in = new Scanner(System.in);   
      
      System.out.println("Enter the String to check if it is a palindrome");  
      
      str = in.nextLine();   
      
      
      int len = str.length();   
      
      
      for ( int i = len - 1; i >= 0; i-- )  {
      
         reverse = reverse + str.charAt(i);  
      }
         
      if (str.equals(reverse))  {
         System.out.println("String is a Palindrome");  
         }
      else  {
         System.out.println("String is not a Palindrome");  
         }
   }  
}  

Input : ABA

Output : String is a Palindrome

Input : HELLO

Output : String is not a Palindrome

Example 2: Check String Palindrome using predefined methods



import java.util.*;   

class CheckPalindromeInJava  
{  

   public static void main(String args[])  
   {  
   
      String str , reverse = "";
      
      Scanner in = new Scanner(System.in);   
      
      System.out.println("Enter the String to check if it is a palindrome");  
      
      str = in.nextLine();   
      
      reverse = new StringBuffer(str).reverse().toString();

         
      if (str.equals(reverse))  {
         System.out.println("String is a Palindrome");  
         }
      else  {
         System.out.println("String is not a Palindrome");  
         }
   }  
}  

Input : MAM

Output : String is a Palindrome

Conclusion

We have learnt different ways of checking the Number and String Palindrome in Java. This is one of the most asked Java interview Question.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply