Reading Time: 3 minutes

Check if a given number is a palindrome or not ?

Palindrome programs are often the first moment where beginners realize that programming is not about syntax, it’s about thinking logically. With just division and modulus, you can solve what looks like a complex problem or If I tell you,  What if you could reverse a number without converting it to a string? The palindrome problem in C teaches exactly that—and it’s more elegant than you think.  In fact the palindrome number problem appears in exams, interviews, and coding tests, but most explanations skip the logic. Let’s break it down visually and mathematically.

A palindrome number is a number that reads the same when written forward and backward. Examples include:

Palindrome means both the original string and the reversed string are one and the same or they are equal. Let’s write a program to check if a given number is a palindrome or not and break down the logic / technique behind it. We do with simple mathematical manipulation and without converting the number into a string.

The Funda

The basic funda is you need to extract the last digit. Extracting the last digit can be done by dividing the number by 10 and then getting the remainder.

Add that last digit obtained, to the reversed number.

Create a new number without the last digit, or remove the last digit because the last digit is used to build reversed number.

and repeat the above steps as long as the number is greater than zero.

Step-by-Step Breakdown

Let us understand the logic clearly.

1. Extracting the Last Digit

To extract the last digit of a number, we use the modulus operator (%).

last_digit = number % 10;
New reversed number can generated by multiplying the reminder number obtained by 10 so that you get the digits reversed And you add the remainder to it.

So that is the whole thing and once you are done with removing that last digit from that number.  You divide the number by 10, if you divide it by 10, and then you get the a new number with the last digit removed.

Example:

This gives us the rightmost digit of the number.

2. Building the Reversed Number

To construct the reversed number, we:

reversed = reversed * 10 + last_digit;


Reversing a number mathematically step by step break diwn

3. Removing the Last Digit from the Original Number

Once the last digit is extracted, we remove it from the number by integer division:

number = number / 10;

This effectively shifts the number to the right:

  • 123 / 10 = 12
  • 12 / 10 = 1
  • 1 / 10 = 0

4. Repeat Until the Number Becomes Zero

We repeat the above steps until the number becomes 0.

At the end of the loop:

  • We will have the reversed number
  • We compare it with the original number

First working code

If we check if this code does what it is supposed to be ?

Yes, It does solve the problem we wanted to solve. But can be improvised.

Improving Code Quality

1. Avoid unnecessary macros


#define TRUE 1
#define FALSE 0

//Instead, we can use this

#include <stdbool.h>

and use bool, true, false.

2. Simplify the function logic

if( n_preserved == rev ){
    palindrome = TRUE;
} else {
    palindrome = FALSE;
}
return palindrome;

// Better option would be

return n_preserved == rev;

3. Performance Considerations

Your algorithm is:

  • Time complexity: O(n), where n is number of digits
  • Space complexity: O(1)

This is optimal for checking if a given number is a palindrome or not.

Possible micro-optimizations:

  • Skip negative numbers.
  • Skip numbers ending in 0 (except 0 itself), since they cannot be palindromes.

Optimized code

Lets find all the Palindrome Numbers less than a given number

A straightforward and maintainable approach is to iterate sequentially from 0 up to a predefined limit, and for each value, delegate the palindrome check to a dedicated function.

The program evaluates each number by invoking the isPalindrome() function. If the function confirms that the current number is a palindrome, the number is immediately displayed. This design keeps the logic clean and modular: the iteration logic remains in main(), while the palindrome-checking logic is encapsulated in a separate function.

In essence, the solution follows a simple workflow:

  1. Loop through all numbers from 0 to the specified limit
  2. Check each number using isPalindrome()
  3. Print the number if it satisfies the palindrome condition

This approach prioritizes clarity, separation of concerns, and ease of understanding, making it especially suitable for beginners and instructional examples.