Palindrome in Java- How to check a number or string?
When people interview for Java, they are normally tested for their logic and programming skills. One of the most frequently asked questions is the Palindrome program in Java. A palindrome is nothing but any number or a string that 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
- Palindrome program using For Loop
- Palindrome program (String) using Library method
Palindrome Program using While Loop
This is one of the easiest programs to find the 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 the same as the reversed number or not. If both the numbers are the 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 a 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 /= 10is executed and conditionnum!=0is checked.
Palindrome Program in Java (String) using Library Method
In this section, we will find the 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 a 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 a string reverse function to calculate the reverse of a number and then compare the same with the original number. If both the numbers are the same, it will print palindrome number, else not a palindrome number.
With this, we come to an end to this blog. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of Java.
5. Java String
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at https://www.edureka.co on June 11, 2019.