Check if the value entered is palindrome or not using C language

A palindrome is a number, word, or sequence of characters that reads the same backward as forward. For example, 121, 1331, and 12321 are palindromic numbers.

To check if a number is a palindrome in C, we reverse the number and compare it with the original. If both are equal, the number is a palindrome.

Syntax

while (n > 0) {
    remainder = n % 10;
    reversed = (reversed * 10) + remainder;
    n = n / 10;
}

Algorithm

The algorithm to check palindrome follows these steps −

  • Store the original number in a temporary variable
  • Extract digits from right to left using modulo operator
  • Build the reversed number by multiplying previous result by 10 and adding current digit
  • Compare the reversed number with the original

Example

Following is the C program to check if a number is palindrome −

#include <stdio.h>

int main() {
    int n, r, sum = 0, temp;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    temp = n;
    
    while (n > 0) {
        r = n % 10;
        sum = (sum * 10) + r;
        n = n / 10;
    }
    
    if (temp == sum)
        printf("It is a palindrome number!");
    else
        printf("It is not a palindrome number!");
    
    return 0;
}

Output

When the above program is executed, it produces the following result −

Enter a number: 121
It is a palindrome number!

Let's test with a non-palindrome number −

Enter a number: 12345
It is not a palindrome number!

How It Works

For input 121:

  • Step 1: temp = 121, sum = 0
  • Step 2: r = 121 % 10 = 1, sum = 0*10 + 1 = 1, n = 121/10 = 12
  • Step 3: r = 12 % 10 = 2, sum = 1*10 + 2 = 12, n = 12/10 = 1
  • Step 4: r = 1 % 10 = 1, sum = 12*10 + 1 = 121, n = 1/10 = 0
  • Result: temp (121) == sum (121), so it's a palindrome

Conclusion

Checking palindrome numbers in C involves reversing the digits and comparing with the original. This approach uses simple arithmetic operations and works efficiently for any integer input.

Updated on: 2026-03-15T13:33:08+05:30

705 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements