C Program – Check Whether a Number is a Palindrome or Not - Mirror Numbers

Check Whether a Number is a Palindrome

This program determines if a given integer is a palindrome. A palindrome retains the same sequence of digits when reversed (e.g., 121 or 5445). We do this by reversing the digits and comparing the result to the original number.

#include <stdio.h>
int main() {
    int n, orig, rev = 0, rem;
    printf("Enter an integer: ");
    scanf("%d", &n);
    orig = n;                     // Save the original number
    while (n != 0) {
        rem = n % 10;             // Extract last digit
        rev = rev * 10 + rem;     // Append to reversed number
        n /= 10;                  // Remove last digit
    }
    if (orig == rev) {
        printf("%d is a palindrome.\n", orig);
    } else {
        printf("%d is not a palindrome.\n", orig);
    }
    return 0;
}
Enter an integer: 1221
1221 is a palindrome.
        

After storing the input in orig, the program reverses the digits in the same way as the reverse-number example. Finally, it compares the reversed value to the original. If they match, the number reads the same forwards and backwards and is therefore a palindrome.

Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗