Palindrome Program in C – Check Number (Iterative, Function, Recursive)

A palindrome program in C checks whether a number reads the same forwards and backwards. Numbers like 121, 1331, and 12321 are palindromes; 12345 is not. The standard technique is to reverse the digits and compare with the original.

This page covers three versions: a simple iterative solution, a clean function-based version, and a recursive approach — plus a variant that validates exactly five digits.

Logic — How to Check a Palindrome Number in C

  1. Save a copy of the original number.
  2. Reverse the number digit by digit: extract the last digit with % 10, shift it into the reversed value, then drop it with / 10.
  3. After the loop, compare the reversed number to the original. Equal → palindrome.

Example trace for 12321:

Step 1: digit = 12321 % 10 = 1,  rev = 0*10 + 1 = 1,  num = 1232
Step 2: digit = 1232  % 10 = 2,  rev = 1*10 + 2 = 12, num = 123
Step 3: digit = 123   % 10 = 3,  rev = 12*10+ 3 = 123, num = 12
Step 4: digit = 12    % 10 = 2,  rev = 123*10+2 = 1232,num = 1
Step 5: digit = 1     % 10 = 1,  rev = 1232*10+1=12321,num = 0
Result: rev == original (12321) → Palindrome

Palindrome Program in C — Iterative Version

#include <stdio.h>

int main(void)
{
    int num, original, reversed = 0, digit;

    printf("Enter an integer: ");
    scanf("%d", &num);

    original = num;

    while (num > 0) {
        digit    = num % 10;
        reversed = reversed * 10 + digit;
        num      = num / 10;
    }

    if (original == reversed)
        printf("%d is a palindrome.\n", original);
    else
        printf("%d is not a palindrome.\n", original);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -o palindrome palindrome.c
./palindrome

Sample Output

Enter an integer: 12321
12321 is a palindrome.

Enter an integer: 12345
12345 is not a palindrome.

Enter an integer: 7
7 is a palindrome.

Function-Based Version — isPalindrome()

Separating the palindrome logic into a function makes the code reusable and easier to test:

#include <stdio.h>

int isPalindrome(int n)
{
    int original = n, reversed = 0, digit;

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

int main(void)
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (isPalindrome(num))
        printf("%d is a palindrome.\n", num);
    else
        printf("%d is not a palindrome.\n", num);

    return 0;
}

Recursive Palindrome Check in C

The recursive approach uses a helper function that builds the reversed number on the call stack:

#include <stdio.h>

int reverseNum(int n, int rev)
{
    if (n == 0)
        return rev;
    return reverseNum(n / 10, rev * 10 + n % 10);
}

int isPalindromeRecursive(int n)
{
    return n == reverseNum(n, 0);
}

int main(void)
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (isPalindromeRecursive(num))
        printf("%d is a palindrome.\n", num);
    else
        printf("%d is not a palindrome.\n", num);

    return 0;
}

How the recursion works: reverseNum(12321, 0) calls itself with reverseNum(1232, 1), then reverseNum(123, 12), and so on. Each call appends one digit to rev until n reaches 0, at which point it returns the fully reversed number.

Five-Digit Palindrome Variant

If you need to validate that the input is exactly five digits before checking:

#include <stdio.h>

int main(void)
{
    int num, original, reversed = 0, digit, count = 0;

    printf("Enter a 5-digit number: ");
    scanf("%d", &num);

    original = num;

    while (num > 0) {
        digit    = num % 10;
        reversed = reversed * 10 + digit;
        num      = num / 10;
        count++;
    }

    if (count != 5)
        printf("Please enter exactly a five-digit number.\n");
    else if (original == reversed)
        printf("%d is a palindrome.\n", original);
    else
        printf("%d is not a palindrome.\n", original);

    return 0;
}

Time and Space Complexity

Version Time Space
Iterative O(d) — d = number of digits O(1)
Recursive O(d) O(d) — call stack depth

Both visit each digit exactly once. The iterative version uses constant space; the recursive version adds one stack frame per digit.

What This Program Teaches

  • The % 10 / / 10 pattern for extracting and consuming digits — a building block for many number-manipulation programs
  • Why you must save the original value before the loop modifies it
  • How to convert an iterative digit-reversal into a tail-recursive form
  • Integer overflow: for large numbers, reversed can overflow int. Using long or checking for overflow is necessary in production code

Related Programs


As an Amazon Associate we earn from qualifying purchases.

Recommended Book

Loops, integer arithmetic, and the modulo operator are all covered in chapter 1 of The C Programming Language by Kernighan & Ritchie. Also on Amazon.com.

1 comment on “Palindrome Program in C – Check Number (Iterative, Function, Recursive)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>