Java Program to Check Palindrome Number
A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 1331, and 12321 are palindrome numbers.
๐ง What is a Palindrome?
Palindrome means the number reads the same forward and backward.
- 121 → Reversed: 121 ✅
- 123 → Reversed: 321 ❌
๐ ️ Logic to Check Palindrome Number
- Take the input number.
- Reverse the number using a loop.
- Compare the reversed number with the original.
- If both are the same, it’s a palindrome.
๐ Java Code
public class PalindromeNumber {
public static void main(String[] args) {
int number = 121;
int original = number;
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
if (original == reversed) {
System.out.println(original + " is a Palindrome number.");
} else {
System.out.println(original + " is not a Palindrome number.");
}
}
}
✅ Output
121 is a Palindrome number.
๐งช Try With Other Numbers
Test with: 12321
, 1331
, 123
๐ Conclusion
This is a basic yet common question in Java interviews. Mastering it will help you understand number manipulation and logic building in Java.