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.
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.
Build real-world apps from scratch and showcase your skills with our hands-on Flutter APP Development Course.
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
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
If you found this article on “Palindrome in Java” relevant, check out the Edureka’s 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.