** How to find if a number is a palindrome in Java**Checking whether a number is palindrome or not is a classical Java homework exercise. When you start learning Java programming most institute which teaches Java programming provides these classical Java programs like How to find Armstrong number in Java or how to find the Fibonacci number in Java using recursion etc. Write a Java program to check if the number is palindrome comes from the same category. For those who are not familiar with palindrome numbers, a palindrome number is a number that is equal to the reverse of itself.

For example, 121 is a palindrome because the reverse of 121 is 121, while 123 is not a palindrome in Java because the reverse of 123 is 321 and 121!=321. Finding a palindrome number is easy in Java, you just need to develop logic to reverse a number in Java.

Thankfully Java provides a couple of arithmetic operators like remainder(%) operator also known as modules operator, which returns remainder in division operator and division operator(/) which returns quotient. By using remainder and division operator in Java we can create program to check if the number is palindrome or not.

## Java program – palindrome numbers in Java

Here is a complete Java program to check if a given number is palindrome or not, This program works for both positive and negative numbers and displays if it’s palindrome irrespective of their sign. Any one-digit number including zero is always palindrome.

This program is able to check two-digit, three-digit numbers for a palindrome and effectively go to any number in Java.

Let’s see how to write a Java program to find palindrome numbers :

```
package testing;
import java.util.Scanner;
/**
* Java program to check if the number is palindrome or not.
* A number is called palindrome if the number
* and its reverse is equal
* This Java program can also be used to reverse a number in Java
*/
public class NoClassDefFoundErrorDueToStaticInitFailure {
public static void main(String args[]){
System.out.println("Please Enter a number : ");
int palindrome = new Scanner(System.in).nextInt();
if(isPalindrome(palindrome)){
System.out.println("Number : " + palindrome
+ " is a palindrome");
}else{
System.out.println("Number : " + palindrome
+ " is not a palindrome");
}
}
/*
* Java method to check if a number is palindrome or not
*/
public static boolean isPalindrome(int number) {
int palindrome = number; // copied number into variable
int reverse = 0;
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
// if original and the reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {
return true;
}
return false;
}
}
Output:
Please Enter a number :
123
Number: 123 is not a palindrome
Please Enter a number :
121
Number: 123 is a palindrome
```

That’s all on **how to check if a number is a palindrome or not in Java**. If you are looking for the answer to this question for interview purposes then better prepare a recursive version of the palindrome program in Java as well because of its common practice that programmer is expected to write java programs using both recursion and iteration in Java. Let me know if you find any other way of checking palindrome in Java.

Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS