Java Program to Check Palindrome Number
while loop and compare it with the original number using if (original == reversed).Examples
How to Think About It
Algorithm
Code
import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); int original = num; int reversed = 0; while (num != 0) { int digit = num % 10; reversed = reversed * 10 + digit; num /= 10; } if (original == reversed) { System.out.println(original + " is a palindrome number."); } else { System.out.println(original + " is not a palindrome number."); } sc.close(); } }
Dry Run
Let's trace the number 121 through the code to see how it checks for palindrome.
Initialize variables
original = 121, num = 121, reversed = 0
First loop iteration
digit = 121 % 10 = 1; reversed = 0 * 10 + 1 = 1; num = 121 / 10 = 12
Second loop iteration
digit = 12 % 10 = 2; reversed = 1 * 10 + 2 = 12; num = 12 / 10 = 1
Third loop iteration
digit = 1 % 10 = 1; reversed = 12 * 10 + 1 = 121; num = 1 / 10 = 0
Loop ends and compare
num = 0, reversed = 121, original = 121; since reversed == original, number is palindrome
| num | digit | reversed |
|---|---|---|
| 121 | 1 | 1 |
| 12 | 2 | 12 |
| 1 | 1 | 121 |
Why This Works
Step 1: Extract digits
Using num % 10 extracts the last digit of the number to build the reversed number.
Step 2: Build reversed number
Multiply the current reversed number by 10 and add the extracted digit to shift digits left and append the new digit.
Step 3: Compare original and reversed
If the reversed number equals the original, the number reads the same forwards and backwards, so it is a palindrome.
Alternative Approaches
import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); String numStr = sc.nextLine(); String reversedStr = new StringBuilder(numStr).reverse().toString(); if (numStr.equals(reversedStr)) { System.out.println(numStr + " is a palindrome number."); } else { System.out.println(numStr + " is not a palindrome number."); } sc.close(); } }
public class PalindromeNumber { public static int isPalindrome(int num, int rev) { if (num == 0) return rev; return isPalindrome(num / 10, rev * 10 + num % 10); } public static void main(String[] args) { int num = 121; if (num == isPalindrome(num, 0)) { System.out.println(num + " is a palindrome number."); } else { System.out.println(num + " is not a palindrome number."); } } }
Complexity: O(d) time, O(1) space
Time Complexity
The loop runs once for each digit in the number, so time complexity is O(d) where d is the number of digits.
Space Complexity
Only a few integer variables are used, so space complexity is O(1), constant space.
Which Approach is Fastest?
The numeric reversal approach is fastest and uses less memory than string reversal or recursion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Numeric reversal | O(d) | O(1) | Efficient and memory-friendly |
| String reversal | O(d) | O(d) | Simple to implement, uses extra memory |
| Recursive check | O(d) | O(d) | Elegant but uses call stack memory |