The numbers 121, 1331 and 45654 read the same from either end. Deciding whether a number has this property is a neat application of digit extraction.

Definition

Definition: Palindrome number. A number that remains the same when its digits are written in reverse order.

The number 1221 is a palindrome. The number 1231 is not, because its reverse is 1321.

The Method

The test follows directly from the definition: build the reverse of the number and compare it with the original. Because digit extraction destroys the number it works on, a copy must be kept for the comparison.

Reversing a Number

The reverse is built one digit at a time. Start with rev = 0. On each pass, take the last digit of the number and attach it to the right of rev:

rev = rev * 10 + digit

Multiplying by 10 shifts the digits already in rev one place to the left, which makes room for the new digit. Table 3.2 traces the reversal of 1231.

Table 3.2: Reversing the number 1231

Pass n before digit rev = rev × 10 + digit n after
1 1231 1 0 × 10 + 1 = 1 123
2 123 3 1 × 10 + 3 = 13 12
3 12 2 13 × 10 + 2 = 132 1
4 1 1 132 × 10 + 1 = 1321 0

The reverse, 1321, differs from 1231, so 1231 is not a palindrome.

Points of Care

Three details need attention.

  • Keep the original. The comparison at the end needs the starting value, so it is saved before the loop.
  • Single-digit numbers. Every number from 0 to 9 is a palindrome. The method handles 1 to 9 correctly; 0 gives rev = 0, which also equals the original.
  • Negative numbers. The task must say how they are treated. The usual convention is that negative numbers are not palindromes, because of the sign.

A number ending in zero, such as 120, can never be a palindrome, since its reverse, 021, would need a leading zero.

Example 3.2: Palindrome test

Problem: Read a positive integer and report whether it is a palindrome.

Solution:

  1. Save the number in original.
  2. Build the reverse in rev using rev = rev * 10 + digit.
  3. Compare rev with original.
#include <stdio.h>

int main(void)
{
    int n, original, digit, rev = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &n);
    original = n;
    while (n > 0)
    {
        digit = n % 10;
        rev = rev * 10 + digit;
        n = n / 10;
    }
    if (rev == original)
    {
        printf("\n%d is a palindrome\n", original);
    }
    else
    {
        printf("\n%d is not a palindrome\n", original);
    }
    return 0;
}

Input:

1221

Output:

Enter a positive integer:
1221 is a palindrome

Result: The reverse of 1221 is built as 1, 12, 122 and 1221. It equals the original, so 1221 is a palindrome.

Common mistakes

  • Comparing rev with n after the loop, when n has already become 0.
  • Writing rev = rev + digit * 10, which does not shift the earlier digits correctly.
  • Not initializing rev to 0 before the loop.

Key takeaways

  • A palindrome number equals its own reverse.
  • The reverse is built with rev = rev * 10 + digit.
  • The original value must be saved before the digits are removed.
  • All single-digit numbers are palindromes.

Practice questions

  1. Is 12021 a palindrome?
  2. What is the value of rev after two passes when reversing 5678?
  3. Why is a copy of the number kept before the loop?

Answers to the practice questions

  1. Yes. Its reverse is 12021, which equals the number.
    1. After pass 1, rev is 8; after pass 2, rev is 8 × 10 + 7 = 87.
  2. The loop reduces the number to 0, and the original value is needed for the final comparison.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III