Palindrome Number Program in Java
In this lesson: Check palindrome number in Java using while loop and reverse — code, dry run and output. Copy & run now. In Hindi & English.
What is a palindrome number
A palindrome number reads the same forwards and backwards — like 121, 1331 or 7. To check, we reverse the number using maths and compare it with the original. If they match, it is a palindrome. It is a classic loop-and-digit exercise.
Reversing a number
We reverse a number with three steps in a loop:
digit = num % 10— get the last digit.rev = rev * 10 + digit— build the reversed number.num = num / 10— remove the last digit.
Repeat until num becomes 0, and rev holds the reversed number.
The Java program
public class Palindrome {
public static void main(String[] args) {
int num = 121;
int original = num; // keep a copy
int rev = 0;
while (num > 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
if (rev == original)
System.out.println(original + " is a palindrome");
else
System.out.println(original + " is not a palindrome");
}
}
We save original, reverse the number into rev, then compare. Since 121 reversed is 121, they match and it is a palindrome.
Dry run
| Step | temp | temp%10 | rev = rev*10 + digit | temp /= 10 |
|---|---|---|---|---|
| 1 | 121 | 1 | 0*10+1 = 1 | 12 |
| 2 | 12 | 2 | 1*10+2 = 12 | 1 |
| 3 | 1 | 1 | 12*10+1 = 121 | 0 |
| 4 | 0 | stops. rev(121) == num(121) → Palindrome! | ||
Each turn peels off the last digit and builds rev with rev*10 + digit. When the number becomes 0, if the reversed value equals the original, it is a palindrome.
A string alternative
int num = 121; String s = String.valueOf(num); String r = new StringBuilder(s).reverse().toString(); System.out.println(s.equals(r) ? "Palindrome" : "Not");
You can also convert the number to a string and compare it with its reverse. The maths method avoids strings, but this is shorter and just as valid.
Common mistakes
- Not saving the original. The loop reduces the number to 0 — keep a copy to compare.
- Wrong reverse formula. Use
rev * 10 + digit, notrev + digit. - Swapping % and /.
% 10gets the digit;/ 10removes it. - Negative numbers. Negatives are usually treated as not palindromes because of the sign.
Summary
- A palindrome number reads the same both ways (121, 1331).
- Reverse the number with
% 10,rev*10 + digit, and/ 10. - Save the original to compare at the end.
- If the reverse equals the original, it is a palindrome.
- A string reverse-and-compare is an easy alternative.
Next, reverse text in reverse a string, or practise digits in the Armstrong number program.
Frequently Asked Questions
What is a palindrome number?
How do I reverse a number in Java?
% 10, build the reversed number with rev = rev * 10 + digit, and remove the last digit with / 10, until the number becomes 0.Why do we keep a copy of the original number?
What does rev = rev * 10 + digit do?
rev by 10 shifts its digits left, and adding the new digit puts it in the ones place, growing the reversed number.Can I check a palindrome number using strings?
str.equals(new StringBuilder(str).reverse().toString()). The maths method avoids strings, but both work.