Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 49

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.

💡 A number is a palindrome if it equals its own reverse — like 121 reversed is still 121.

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");
    }
}
121 is 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

Dry Run — is 121 a palindrome? (reverse the number)
Steptemptemp%10rev = rev*10 + digittemp /= 10
112110*10+1 = 112
21221*10+2 = 121
31112*10+1 = 1210
40stops. rev(121) == num(121) → Palindrome!
💡 Understand it this way

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");
Palindrome

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, not rev + digit.
  • Swapping % and /. % 10 gets the digit; / 10 removes 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?
A palindrome number reads the same forwards and backwards, like 121 or 1331. To check, you reverse the number and see if it equals the original. If it does, it is a palindrome.
How do I reverse a number in Java?
Use maths: repeatedly take the last digit with % 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?
The reversing loop changes the number down to 0, so you lose it. Saving the original in another variable lets you compare it with the reversed value at the end.
What does rev = rev * 10 + digit do?
It builds the reversed number digit by digit. Multiplying 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?
Yes. Convert the number to a string and compare it with its reverse: str.equals(new StringBuilder(str).reverse().toString()). The maths method avoids strings, but both work.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicArmstrong Number Your progress: 49 / 61 (80%) · Lesson 50 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free