0
0
CProgramBeginner · 2 min read

C Program to Check Palindrome Number

A C program to check palindrome number reverses the number using a loop and compares it with the original number using while and if statements; if both are equal, it prints the number is palindrome.
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
🧠

How to Think About It

To check if a number is palindrome, think of reversing the digits one by one and then comparing the reversed number with the original. If they match exactly, the number reads the same forwards and backwards, so it is a palindrome.
📐

Algorithm

1
Get the input number from the user.
2
Store the original number in a temporary variable.
3
Initialize a variable to hold the reversed number as 0.
4
Use a loop to extract the last digit of the number and build the reversed number.
5
Compare the reversed number with the original number.
6
Print if the number is palindrome or not based on the comparison.
💻

Code

c
#include <stdio.h>

int main() {
    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 /= 10;
    }
    if (original == reversed)
        printf("%d is a palindrome number.\n", original);
    else
        printf("%d is not a palindrome number.\n", original);
    return 0;
}
Output
Enter an integer: 121 121 is a palindrome number.
🔍

Dry Run

Let's trace the input 121 through the code

1

Input and Initialization

num = 121, original = 121, reversed = 0

2

First loop iteration

digit = 121 % 10 = 1; reversed = 0 * 10 + 1 = 1; num = 121 / 10 = 12

3

Second loop iteration

digit = 12 % 10 = 2; reversed = 1 * 10 + 2 = 12; num = 12 / 10 = 1

4

Third loop iteration

digit = 1 % 10 = 1; reversed = 12 * 10 + 1 = 121; num = 1 / 10 = 0

5

Comparison

original = 121, reversed = 121; they are equal

numdigitreversed
12111
12212
11121
💡

Why This Works

Step 1: Extract digits

The code uses num % 10 to get the last digit of the number.

Step 2: Build reversed number

It multiplies the current reversed number by 10 and adds the extracted digit to shift digits left and append the new digit.

Step 3: Compare original and reversed

If the reversed number equals the original, the number is a palindrome.

🔄

Alternative Approaches

String conversion
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[20];
    int len, i, flag = 1;
    printf("Enter an integer: ");
    scanf("%s", str);
    len = strlen(str);
    for (i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            flag = 0;
            break;
        }
    }
    if (flag)
        printf("%s is a palindrome number.\n", str);
    else
        printf("%s is not a palindrome number.\n", str);
    return 0;
}
This method treats the number as a string and compares characters from both ends; it is simpler but uses extra memory for the string.
Recursive reversal
c
#include <stdio.h>

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

int main() {
    int num, rev;
    printf("Enter an integer: ");
    scanf("%d", &num);
    rev = reverse(num, 0);
    if (num == rev)
        printf("%d is a palindrome number.\n", num);
    else
        printf("%d is not a palindrome number.\n", num);
    return 0;
}
This uses recursion to reverse the number; it is elegant but may use more stack space for large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so time depends on the number of digits d, making it O(d).

Space Complexity

It uses a fixed number of variables regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The numeric reversal method is fastest and uses least memory compared to string conversion or recursion.

ApproachTimeSpaceBest For
Numeric reversalO(d)O(1)Fast and memory efficient
String conversionO(d)O(d)Simple code, uses extra memory
Recursive reversalO(d)O(d)Elegant but uses stack space
💡
Always store the original number before reversing to compare later.
⚠️
Forgetting to reset or store the original number before modifying it causes wrong palindrome checks.