Placement Prep

Check Whether a Number is Palindrome or Not

How to check whether a number is palindrome or not, with iterative and recursive programs in Python, C, and Java, plus complexity analysis.

By FACE Prep Team 6 min read
palindrome-number number-programs python-programs data-structures placement-prep coding-interview

A number is a palindrome when reversing its digits produces the original number. The programs below implement two approaches: an iterative loop and a recursive call chain.

What Is a Palindrome Number?

The word palindrome comes from Greek; it describes any sequence that reads the same forwards and backwards. For numbers, the check is purely digit-by-digit: reverse the digits, then compare with the original.

Clear examples:

  • 121 reversed is 121 — palindrome.
  • 1331 reversed is 1331 — palindrome.
  • 12321 reversed is 12321 — palindrome.
  • 123 reversed is 321 — not a palindrome.
  • 100 reversed is 001, which equals 1 — not a palindrome.

A few edge cases matter in coding rounds:

  • 0 through 9: all single-digit numbers are palindromes by definition. No reversal can differ from a one-digit original.
  • Negative numbers: not palindromes. The minus sign does not reverse symmetrically, so the check returns false for any n < 0.
  • Numbers ending in zero (except 0 itself): never palindromes. Any number with a trailing zero, when reversed, starts with zero, which collapses to a smaller number that cannot equal the original.

Algorithm: The Digit-Extraction Loop

The iterative palindrome check uses the same digit-by-digit extraction mechanic as the sum-of-digits program. The difference is in what you do with each digit: instead of adding it to a running total, you append it to a reversed number.

The core logic:

  • Step 1: Save the original number as original = n.
  • Step 2: Initialise rev = 0.
  • Step 3: While n > 0, extract digit = n % 10, update rev = rev * 10 + digit, shrink n = n // 10.
  • Step 4: After the loop, rev holds the digit-reversed number. If rev == original, return true. Otherwise return false.

Why each operation works:

  • n % 10 gives the last digit because modulo 10 returns the remainder after dividing by 10.
  • rev * 10 + digit appends the digit to the right of the accumulated reverse: multiplying by 10 shifts existing digits left by one place, then the new digit occupies the units position.
  • n // 10 strips the last digit via integer division.

After each iteration, one digit has migrated from n to rev. The loop ends when n reaches zero and all digits have been transferred.

The trailing-zero case fails naturally here. For 100: first iteration extracts digit = 0, sets rev = 0, then n = 10. Second iteration extracts digit = 0, sets rev = 0, then n = 1. Third iteration extracts digit = 1, sets rev = 1, then n = 0. Final comparison: 1 != 100. Correct result without any special-case handling.

Iterative Approach

# Python
def is_palindrome(n):
    if n < 0:
        return False
    original = n
    rev = 0
    while n > 0:
        digit = n % 10
        rev = rev * 10 + digit
        n //= 10
    return rev == original
/* C */
#include <stdio.h>

int is_palindrome(int n) {
    if (n < 0) return 0;
    int original = n, rev = 0;
    while (n > 0) {
        int digit = n % 10;
        rev = rev * 10 + digit;
        n /= 10;
    }
    return rev == original;
}

int main() {
    printf("%s\n", is_palindrome(121) ? "Palindrome" : "Not palindrome");
    printf("%s\n", is_palindrome(123) ? "Palindrome" : "Not palindrome");
    return 0;
}
// Java
public class PalindromeNumber {
    public static boolean isPalindrome(int n) {
        if (n < 0) return false;
        int original = n, rev = 0;
        while (n > 0) {
            int digit = n % 10;
            rev = rev * 10 + digit;
            n /= 10;
        }
        return rev == original;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome(121) ? "Palindrome" : "Not palindrome");
        System.out.println(isPalindrome(123) ? "Palindrome" : "Not palindrome");
    }
}

Trace for 121

  • Initial: original = 121, rev = 0, n = 121
  • Iteration 1: digit = 121 % 10 = 1, rev = 0 * 10 + 1 = 1, n = 121 / 10 = 12
  • Iteration 2: digit = 12 % 10 = 2, rev = 1 * 10 + 2 = 12, n = 12 / 10 = 1
  • Iteration 3: digit = 1 % 10 = 1, rev = 12 * 10 + 1 = 121, n = 1 / 10 = 0
  • Loop exits. rev = 121, original = 121. Equal — palindrome.

Trace for 123

  • Initial: original = 123, rev = 0, n = 123
  • Iteration 1: digit = 3, rev = 3, n = 12
  • Iteration 2: digit = 2, rev = 32, n = 1
  • Iteration 3: digit = 1, rev = 321, n = 0
  • Loop exits. rev = 321, original = 123. Not equal — not a palindrome.

Recursive Approach

The recursive version replaces the iterative rev variable with function arguments, passing the accumulator down the call chain. When n reaches 0, the recursion bottoms out and returns the accumulated reverse.

# Python
def reverse_number(n, rev=0):
    if n == 0:
        return rev
    return reverse_number(n // 10, rev * 10 + n % 10)

def is_palindrome_recursive(n):
    if n < 0:
        return False
    return reverse_number(n) == n
/* C */
int reverse_number(int n, int rev) {
    if (n == 0) return rev;
    return reverse_number(n / 10, rev * 10 + n % 10);
}

int is_palindrome_recursive(int n) {
    if (n < 0) return 0;
    return reverse_number(n, 0) == n;
}
// Java
public static int reverseNumber(int n, int rev) {
    if (n == 0) return rev;
    return reverseNumber(n / 10, rev * 10 + n % 10);
}

public static boolean isPalindromeRecursive(int n) {
    if (n < 0) return false;
    return reverseNumber(n, 0) == n;
}

Trace for 121 (recursive)

  • reverseNumber(121, 0) calls reverseNumber(12, 1)
  • reverseNumber(12, 1) calls reverseNumber(1, 12)
  • reverseNumber(1, 12) calls reverseNumber(0, 121)
  • reverseNumber(0, 121) returns 121 (base case reached)
  • Back in isPalindromeRecursive: 121 == 121. Result: palindrome.

The logic is identical to the iterative version. The call stack stores the intermediate rev values in place of the loop variable.

Edge Cases Worth Knowing

These come up in placement tests and interview discussions.

  • n = 0: reverseNumber(0, 0) hits the base case immediately and returns 0. 0 == 0 — palindrome.
  • n = 10: First extraction gives digit = 0, rev = 0. Second gives digit = 1, rev = 1. Final: 1 != 10 — not a palindrome.
  • n = 11: Reversed is 11. 11 == 11 — palindrome.
  • n = -121: The negative guard short-circuits before any reversal. Returns false.
  • Large numbers: For int in C and Java, reversing a very large number can overflow. The half-reversal technique reverses only the second half of the number and compares it with the first half. This avoids overflow and is what LeetCode Problem 9: Palindrome Number explicitly asks for.

Time and Space Complexity

ApproachTime complexitySpace complexity
IterativeO(d)O(1)
RecursiveO(d)O(d)

Here d is the number of digits in the input. Each iteration (or recursive call) processes exactly one digit, so both approaches are O(d) in time. The difference is in space: the iterative version uses a fixed set of variables regardless of input size: O(1). The recursive version creates one stack frame per digit, so it uses O(d) extra space. For a broader treatment of how call-stack space costs accumulate across algorithm types, see the space complexity guide.

Palindrome Problems in Placement Rounds

Palindrome number checks are a standard problem in TCS NQT and Infosys InfyTQ coding sections, and they appear in campus placement drives across CSE, ECE, and IT branches. LeetCode Problem 9: Palindrome Number is the canonical online judge form; it adds one constraint: no string conversion allowed, which rules out the simple str(n) == str(n)[::-1] shortcut.

Common variations in placement rounds:

  • Find all palindrome numbers in a given range (wrap the check in a loop over that range).
  • Print the nearest palindrome greater than a given number.
  • Count palindromes below a certain threshold and compute their digit sums (combines this check with a perfect number or digit-sum pattern).

The digit-extraction loop (% 10, // 10) behind the palindrome check is one of the most reused patterns in number-manipulation problems. It appears in integer-reversal, digit-sum, and happy-number problems on every major coding platform. That same modulo-and-divide mechanic also shows up in data-preprocessing pipelines for LLM applications, where raw numeric inputs need digit-level validation before feeding into a model. If you want to see that pattern scale into real build tasks, TinkerLLM runs hands-on projects at ₹299 that start from number manipulation and progress to deployed applications.

Primary sources

Frequently asked questions

Is 0 a palindrome number?

Yes. Reversing 0 gives 0, so it satisfies the palindrome condition.

Are negative numbers considered palindromes?

No. The conventional check treats the minus sign as part of the number, so -121 reversed reads as 121-, which does not equal -121.

What is the time complexity of the palindrome number check?

O(d), where d is the number of digits. Each loop iteration or recursive call processes exactly one digit.

What happens with numbers ending in 0, like 100?

100 is not a palindrome. Its reverse is 001, which equals 1, and 1 is not equal to 100. Trailing zeros always produce a mismatch.

Are all single-digit numbers palindromes?

Yes. Any single-digit number from 0 through 9 is trivially a palindrome because it reads the same in both directions.

Can I check palindrome without fully reversing the number?

Yes. One optimisation reverses only the second half of the number and compares it with the first half. This avoids integer-overflow risk on very large numbers and is the approach LeetCode Problem 9 recommends.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF