Java – Check Palindrome Number
A palindrome number reads the same from left to right and from right to left. For example, 12521 is a palindrome because reversing its digits still gives 12521. The number 12651 is not a palindrome because its reverse is different.
In Java, a palindrome number can be checked in several ways. You can reverse the number and compare the reversed value with the original, compare matching digits from the two ends, or reverse the digits mathematically with a loop. This tutorial shows these approaches and explains when each one is useful.
How a Palindrome Number Check Works in Java
The basic condition is simple: the original number and its reversed form must be equal. For a positive integer such as 12321, the reverse is also 12321, so the number is a palindrome. For 12345, the reverse is 54321, so it is not a palindrome.
When checking an integer programmatically, keep the original value unchanged if you plan to compare it after reversing the digits. If the program accepts user input, read the number first, store a copy, reverse the working value, and then compare the two values.
Check Palindrome Number – Compare Original and Reverse
A number could be a palindrome if it equals the reverse of it.
In the following example, we shall reverse the given number in Java using StringBuilder, and compare it with its original value. If both are equal, then we can say that the given number is a palindrome.
PalindromeNumber.java
/**
* Java Program - Check if Number is Palindrome
*/
public class PalindromeNumber {
public static void main(String[] args) {
int n = 1236321;
//reverse the string
int rev = Integer.parseInt((new StringBuilder(n+"")).reverse().toString());
//check if n is palindrome
if(n==rev) {
System.out.println(n+" is Palindrome.");
} else {
System.out.println(n+" is not Palindrome.");
}
}
}
Output
1236321 is Palindrome.
This approach converts the integer to text, reverses that text with StringBuilder.reverse(), converts the result back to an integer, and compares the two integer values. It is concise and easy to read for normal integer inputs.
Check Palindrome Number Using a While Loop and Scanner
A common Java exercise is to check a palindrome number using user input and a while loop. This version reverses the number mathematically, without converting it to a string.
Palindrome Number Logic with Remainder and Division
For each iteration, number % 10 gets the last digit, and number / 10 removes that digit. The reversed number is built with reverse = reverse * 10 + digit.
import java.util.Scanner;
public class PalindromeUsingWhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int number = scanner.nextInt();
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (original == reverse) {
System.out.println(original + " is a palindrome number.");
} else {
System.out.println(original + " is not a palindrome number.");
}
scanner.close();
}
}
If the input is 1221, the loop builds the reversed value as 1221. Since it matches the original value, the program reports that the number is a palindrome.
Enter a non-negative integer: 1221
1221 is a palindrome number.
For input 1234, the reversed value becomes 4321, so the comparison fails.
Enter a non-negative integer: 1234
1234 is not a palindrome number.
Check if Number is Palindrome by Checking at Digit Level
Instead of reversing the number and checking for equality, we can check if the first and last digits are equal and progress to the middle of the number. If at any point, the digits are not equal, then it is not a palindrome.
Algorithm for Comparing Palindrome Digits from Both Ends
We shall implement following algorithm in Java and write a program to check if given number is palindrome.
- Start.
- Take the number in n. We need to check if this is palindrome number or not.
- Take a boolean variable isPalindrome to store if the number is palindrome or not. Initialize it with
true. - Initialize variable i with
0. - Check if i is less than half the number of digits in n. If yes, go to step 6, else go to step 8.
- Check if digit in n at index i is equal to that of at
length-1-i. If not set isPalindrome tofalseand go to step 8. - Increment i. Go to step 5.
- Based on the value of isPalindrome, print the result.
- Stop.
PalindromeNumber.java
import java.lang.Math;
/**
* Java Program - Check if Number is Palindrome
*/
public class PalindromeNumber {
public static void main(String[] args) {
int n = 1236321;
//reverse the string
int length = (n+"").length();
boolean isPalindrome = true;
//check if ith digit is same from start and end
for(int i=0;i<length/2;i++) {
if( (n/(int)Math.pow(10, length-1-i))%10 != (n/(int)Math.pow(10, i))%10) {
isPalindrome = false;
break;
}
}
//check if str is palindrome
if(isPalindrome) {
System.out.println(n+" is Palindrome Number.");
} else {
System.out.println(n+" is not Palindrome Number.");
}
}
}
Output
1236321 is Palindrome Number.
This method compares corresponding digits rather than building a complete reversed number. The loop only needs to check up to half of the digits, because every digit in the first half is paired with a digit in the second half.
Palindrome Number in Java Using a For Loop
The same arithmetic reversal can be written with a for loop. This is useful when you prefer to keep the loop initialization, condition, and update in one statement.
public class PalindromeUsingForLoop {
public static void main(String[] args) {
int number = 454;
int original = number;
int reverse = 0;
for (; number > 0; number /= 10) {
int digit = number % 10;
reverse = reverse * 10 + digit;
}
if (original == reverse) {
System.out.println(original + " is a palindrome number.");
} else {
System.out.println(original + " is not a palindrome number.");
}
}
}
454 is a palindrome number.
Palindrome Number Edge Cases in Java
- Single-digit numbers: Values from
0through9are palindromes because reversing one digit does not change the value. - Numbers ending in zero: A positive number such as
120is not a palindrome, because its reversed digit sequence is021, which represents21as an integer. - Negative numbers: Decide the rule your program should follow. In the examples above, the arithmetic loop is intended for non-negative integers. If the minus sign is considered part of the representation, negative integers are not palindromes.
- Large integer values: When reversing digits arithmetically, the reversed value can exceed the range of
int. Uselongor add an overflow check when the input range can be large.
Choosing a Java Palindrome Number Approach
| Approach | How it works | Useful when |
|---|---|---|
| StringBuilder reverse | Convert the number to text, reverse it, and compare | You want concise, readable code |
| While loop | Reverse digits with % 10 and / 10 | You want a numeric solution without string conversion |
| For loop | Uses the same numeric reversal logic in a for loop | You specifically want a for-loop implementation |
| Digit-by-digit comparison | Compare digits from the two ends toward the middle | You want to avoid constructing a full reversed value |
Editorial QA Checklist for Java Palindrome Number Examples
- Confirm that each example compares against the original number rather than a value already modified by the loop.
- Test at least one palindrome such as
1221and one non-palindrome such as1234. - Check how the example handles
0, single-digit values, numbers ending in zero, and negative input. - If arithmetic reversal uses
int, verify that the chosen sample cannot overflow while building the reversed number. - Keep output text consistent with the actual result shown by the code.
Java Palindrome Number Checks: Key Takeaways
A Java program can check whether a number is a palindrome by reversing the number and comparing it with the original, or by comparing matching digits from both ends. A while loop with Scanner is a straightforward choice for user input, while StringBuilder offers a compact string-based solution. In this Java Tutorial, we have written Java program using different techniques on how to check if given number is a palindrome or not.
TutorialKart.com