Java/J2EE and SOA (185 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (47 Blogs)
  • Java/J2EE and SOA (185 Blogs)
  • Spring Framework (7 Blogs)
  • PHP and MySQL (24 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Palindrome in Java: How to check a number is palindrome?

Last updated on Jun 26,2019 1.3K Views
Aayushi Johari
A technophile who likes writing about different technologies and spreading knowledge. A technophile who likes writing about different technologies and spreading knowledge.
3 / 14 Blog from Java Programs

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

When people interview for Java, they are normally tested for their logic and programming skills. One of the most frequently asked questions is Palindrome program in Java. Palindrome is nothing but any number or a string which remains unaltered when reversed. For example: 12321 or MAAM. It is evident that letters form mirror images on reversal.

I have covered the following aspects which demonstrate multiple ways to check Palindrome in Java:

Palindrome Program using While Loop

This is one of the easiest programs to find Palindrome program using ‘For Loop’. Let’ dive into an example to check whether a given input is a palindrome or not.

public class PalindromeProgram {

    public static void main(String[] args) {

        int rem, rev= 0, temp;
	int n=121; // user defined number to be checked for palindrome 

        temp = n;

        // reversed integer is stored in variable 
        while( n != 0 )
        {
            rem= n % 10;
            rev= rev * 10 + rem;
            n=n/10;
        }

        // palindrome if orignalInteger(temp) and reversedInteger(rev) are equal
        if (temp == rev)
            System.out.println(temp + " is a palindrome.");
        else
            System.out.println(temp + " is not a palindrome.");
    }
}

Output: 121 is a palindrome number

Explanation: Input the number you want to check and store it in a temporary(temp) variable. Now reverse the number and compare whether the temp number is same as the reversed number or not. If both the numbers are same, it will print palindrome number, else not a palindrome number.

Note: The logic of the Palindrome program remains the same, but the execution differs.

Now that you are clear with the logic, let’s try to implement the palindrome program in Java in another way i.e using while loop.

Palindrome program using For Loop

public class PalindromeProgram {
 
    public static void main(String[] args) {
 
        int n=1234521, rev=0, rem, temp;
 
        temp = n;
 
        for( ;n != 0; n /= 10 )
        {
            rem = n % 10;
            rev= rev* 10 + rem;
        }
 
        // palindrome if temp and sum are equal
        if temp== rev)
            System.out.println(temp + " is a palindrome.");
        else
            System.out.println(temp + " is not a palindrome.");
    }
}

Output: 1234521 is not a palindrome

Explanation: In the above program, the number is not a palindrome. The logic remains the same, just ‘for’ loop is used instead of a while loop. On each iteration, num /= 10 is executed and condition num!=0 is checked.

 

Palindrome Program in Java (String) using Library Method

In this section, we will find palindrome of a Java string. It works in the same way as that of integers, For example, “madam” is a palindrome, but “madame” is not a palindrome. Let’s implement this palindrome program in Java using string reverse function.

class PalindromeProgram
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();

// checks whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes, it is a palindrome");

else
System.out.println("No, it is not a palindrome");
}

public static void main (String[] args)
throws java.lang.Exception
{
checkPalindrome("madam");
}
}

Output: Yes, it is a palindrome

Explanation: In the above code, we have used string reverse function to calculate the reverse of a number and then compare the same with the original number. If both the numbers are same, it will print palindrome number, else not a palindrome number.

 

This brings us to the end of this article where we have learned how to find palindrome in Java. Hope you are clear with all that has been shared with you in this tutorial.
Make sure you practice as much as possible and revert your experience.  

If you found this article on “Palindrome in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Palindrome in Java” and our team will be glad to answer.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.