Java program to check palindrome


The term palindrome is derived from the Greek word 'palin dromo' which means running back again. A number, string or phrase that remains the same when reversed i.e. reads the same backward as forwards is called a palindrome. For instance, numerical palindromes include numbers like 525, 2002 and 12121. And, word palindromes are 'ABA', 'BABAB' and 'RADAR'. To check palindrome in Java, we can use the while loop and if-else block that will identify whether the given numbers or strings and their reverse are the same or not.

Java Program to Check Plaindrome

In this section, we will write different Java programs to check whether the given input is palindrome or not. Before that let's discuss the problem statement with the help of an example −

Instance

Input 1

121

Output

121 is a Palindrome

Input 2

34431

Output

34431 is not a Palindrome

Input 3

"ABABA"

Output

ABABA is a Palindrome

Now, let's jump into the Java programs to check palindrome.

Example 1

In this example, we will get a number as input from the user and check whether that number is a Palindrome or not. We will also take the help of while loop and if-else conditional block.

import java.util.*;
public class Example1 {
   public static void main(String[] args) {
      // creating an instance of Scanner class
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter a number to check palindrome: ");
      // to take input from user
      int num = sc.nextInt();
      // copying the original input 
      int copy = num;
      // variable to store the result
      int revVal = 0;
      // loop to check palindrome
      while(copy != 0) {
         int rem = copy % 10; // extracting remainder
         copy /= 10; // dividing and storing the number 
         revVal = revVal * 10 + rem; // reversing 
      }
      // checking whether the reverse and original number is same or not
      if(num == revVal) {
         System.out.println(num + " is a Palindrome number");
      } else {
         System.out.println(num + " is not a Palindrome number");
      }
   }
}

Output 1

Enter a number to check palindrome: 
323
323 is a Palindrome number

Output 2

Enter a number to check palindrome: 
258
258 is not a Palindrome number

Example 2

The following example illustrates how we can check if a string is palindrome or not. We will use the built-in method named reverse() of StringBuffer class to reverse the given string. Then, we pass this reversed string to equals() method to if check both original and reversed strings are same or not. If both are the same then, string is palindrome otherwise not.

public class Example2 {
   public static void main (String[] args) {
      String str = "ABABA";
      System.out.println("The original String is: " + str);
      // reversing the original string
      String strRev = new StringBuffer(str).reverse().toString();
      // checking whether the reverse is equal to the original string
      if (str.equals(strRev)) {
         System.out.println(str + " is a Palindrome!");
      } else {
         System.out.println(str + " is not a Palindrome!");
      }
   }
}

Output

The original String is: ABABA
ABABA is a Palindrome!

Example 3

This is another Java program to check if a given number is palindrome or not.

public class Example3 {
   public static void main(String[] args) {
      int num = 52525;
      System.out.println("The original number is: " + num);
      // copying the original input 
      int copy = num;
      // variable to store the result
      int revVal = 0;
      // loop to check palindrome
      while(copy != 0) {
         int rem = copy % 10; // extracting remainder
         copy /= 10; // dividing and storing the number 
         revVal = revVal * 10 + rem; // reversing 
      }
      // checking whether the reverse and original number is same or not
      if(num == revVal) {
         System.out.println(num + " is a Palindrome number");
      } else {
         System.out.println(num + " is not a Palindrome number");
      }
   }
}

Output

The original number is: 52525
52525 is a Palindrome number

Conclusion

In this article, we have learned about palindrome in Java. Also, we discussed different Java programs that illustrate how to check if a given input is palindrome or not. The most straightforward way to achieve this is by using the built-in method of StringBuffer class named reverse().

Updated on: 10-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements