Java ProgramsNumbersCheck Palindromic Number

Check Palindromic Number in Java

beginner·  Numbers  ·  Number Theory

Problem

A palindromic number is a number that reads the same forwards and backwards, the same idea as a palindromic string but checked with pure arithmetic instead of text.

Given a number, determine whether it is a palindrome.

Input
12321
Output
12321 is a palindrome: true

Java Program

Java
public class PalindromeNumberCheck { public static void main(String[] args) { int num = 12321; int original = num; int reversed = 0; while (num > 0) { int digit = num % 10; // read off the last digit reversed = reversed * 10 + digit; // shift left and append the digit num /= 10; } System.out.println(original + " is a palindrome: " + (reversed == original)); } }

Output

12321 is a palindrome: true

Core Logic

Rebuilding the number in reverse, one digit at a time using % and /, and comparing the result to the original checks the definition without ever touching a String.

How It Works
  1. 1original keeps a copy of the starting value, since num gets consumed digit by digit.
  2. 2Each loop pass reads the last digit with num % 10, then shifts reversed left by one decimal place and adds that digit: reversed = reversed * 10 + digit.
  3. 3num /= 10 removes the digit that was just processed.
  4. 4Once num reaches 0, every digit has been moved into reversed in reverse order, and it's compared against original.
For 12321, digits 1, 2, 3, 2, 1 are read off in that order and rebuilt into reversed = 12321 — an exact match.
💡

Key Point: This is the arithmetic counterpart to reversing a string — the same digit-by-digit idea, just built from % and / instead of charAt().

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The loop runs once per digit, and only the running reversed value and the original are kept, regardless of how large the number is.

Key Concepts

modulo operatordigit reversalwhile loop

Approach 2: String Reversal

Java
public class PalindromeNumberString { public static void main(String[] args) { int num = 12321; String str = String.valueOf(num); // Reuses StringBuilder's built-in reverse(), the same technique used for string palindromes String reversed = new StringBuilder(str).reverse().toString(); System.out.println(num + " is a palindrome: " + str.equals(reversed)); } }

Output

12321 is a palindrome: true

Core Logic

Converting the number to a String and reusing StringBuilder's built-in reverse() sidesteps the arithmetic entirely.

How It Works
  1. 1String.valueOf(num) converts the number into its text representation.
  2. 2new StringBuilder(str).reverse().toString() flips the character order, the same technique used to reverse a string elsewhere on this site.
  3. 3str.equals(reversed) compares the original and reversed text directly.
For 12321, the string "12321" reverses to "12321" — identical, so the check returns true.
💡

Key Point: This is shorter to write than the arithmetic version, at the cost of converting the number to a String and back — worth it for a one-off check, less so in a tight loop over many numbers.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: Converting to a String and reversing it both allocate new objects proportional to the number's digit count, unlike the arithmetic version's constant extra space.

Key Concepts

StringBuilder.reverse()String comparison

Related Programs