✓ Link copied to clipboard
C 📅 July 6, 2026 ⏱ 5 min read

C Program to Check Whether a Number is Palindrome or Not

C Program to Check Whether a Number is Palindrome or Not

Checking if a number is a palindrome can be pretty useful. For example, 121 reads the same way forward and backward, so it’s a palindrome. How can we do this in a C program? Let’s dive in.

Prerequisites for the Palindrome Program in C

Before diving into the code, ensure you're familiar with these:

  • Using while loops for iteration.
  • The modulo operator % for obtaining remainders.
  • Basic concepts of reversing numbers.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Check Whether a Number is Palindrome or Not

Below is the full code to check if a given number is a palindrome.

palindrome.cC▶ Run
#include <stdio.h>

int main() {
    int n, original, reverse = 0, rem;
    // Input number from user
    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;
    // Process each digit of the number
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }
    // Check if original and reversed numbers are the same
    if (original == reverse)
        printf("%d is a palindrome.\n", original);
    else
        printf("%d is not a palindrome.\n", original);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Palindrome Program

Let's see what happens when we run the code with an example input.

Outputtext
Enter a number: 121
121 is a palindrome.
Output of C Program to Check Whether a Number is Palindrome or Not
Output of C Program to Check Whether a Number is Palindrome or Not

Algorithm to Check a Number for Palindrome in C

A palindrome reads the same forwards and backwards. Here's how this program figures it out:

  1. Read the integer n from the user.
  2. Initialize original = n and reverse = 0.
  3. While n isn't zero, do the following:
  4. Get the last digit rem = n % 10.
  5. Update the reverse number reverse = reverse * 10 + rem.
  6. Remove the last digit from n by dividing it by 10.
  7. Finally, compare original and reverse.
  8. If they match, the number is a palindrome; otherwise, it's not.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[original = n]
  C --> D[reverse = 0]
  D --> E{"n != 0?"}
  E -->|Yes| F["rem = n %% 10"]
  F --> G["reverse = reverse * 10 + rem"]
  G --> H[n = n / 10]
  H --> E
  E -->|No| I{"original == reverse?"}
  I -->|Yes| J([Print "Palindrome"]) 
  I -->|No| K([Print "Not a palindrome"]) 
  J --> L([End])
  K --> L
Flowchart: the algorithm step by step

How the Palindrome Check Works

Understanding the line-by-line workings of this code will demystify how it checks for palindromes.

  • The program begins by including the <stdio.h> library, essential for input and output functions.
  • We declare int variables: n, original, reverse, and rem to store various values.
  • We prompt the user to enter a number using printf, and capture it with scanf.
  • Store the original number to original for final comparison.
  • A while loop runs as long as n is not zero, processing one digit at a time.
  • Within the loop, the last digit is extracted using rem = n % 10.
  • We update reverse by appending the extracted digit, effectively reversing the number.
  • The number n is then divided by 10 to remove the last processed digit, preparing for the next iteration.
  • Once all digits are processed, we compare the original number with reverse.
  • If they match, printf confirms the number is a palindrome; otherwise, it's not.
  • The program ends by returning 0, indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the key check to determine if a number is a palindrome?

🎉Correct! The key check is whether the original number equals its reverse.
🤔Not quite. Think about the definition of a palindrome.

How Fast is the Palindrome Check in C?

Let's examine the efficiency of this palindrome checking program.

Time Complexity: O(d). Here, d is the number of digits in the number being checked — 3 for 121. The loop while (n != 0) runs once per digit, making the process linear to the digit count. So, 5 digits mean 5 rounds through the loop.

Space Complexity: O(1). Though it processes an entire number, it uses a constant amount of additional space: n, original, reverse, and rem — four integers, no matter how many digits.

⚠️

Common mistake:Not initializing reverse to zero can mess up your results.

Key takeaway:A palindrome check means reversing the digits, so it's all about comparing the original and the reverse.

FAQs About Checking a Number for Palindrome in C

Here are some common questions about this program, answered simply.

Numbers that read the same forwards and backwards, like 121 or 12321.
In this program, negative numbers aren't palindromes because the minus sign changes how they read backwards.
The limit depends on the integer type, but generally, it handles up to 10 digits for a standard int type.
Nope, the program only takes integer input.
One way is to turn the number into a string and compare characters, but that needs more space for strings.

If you found this program helpful, check out these related programs:

Conclusion

Palindrome checks are a classic exercise in reversing logic. The same idea pops up in many real-world scenarios, from computer science to genetics. Into strings? Give writing a program that checks palindrome words a shot next!

Keep practicing with more C puzzles and challenge yourself in our language quiz. Or check out more in the online compiler.