Palindrome in Java- How to check a number or string?

Swatee Chand
Edureka
Published in
4 min readJun 11, 2019

--

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.

1. Java Tutorial

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

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

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on June 11, 2019.

--

--