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

Last updated on Jun 19,2023 43K Views

A technophile with a passion for unraveling the intricate tapestry of the...
1 / 29 Blog from Java Programs

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.

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

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
{
}
}```

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’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.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 10th August,2024

10th August

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details