C Palindrome Number

A number is a Palindrome, if given number and its reversed value are same.

In this tutorial, we will write a C Program, where we read a number from user, and check if this number is a Palindrome or not.

C Program

In the following program, we read a number into n, and check if this number n is a Palindrome or not.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number : ");
    scanf("%d", &n);
    
    //reverse n and store in reversed
    int reversed = 0;
    int remainder;
    int n_temp = n;
    while(n_temp != 0) {
        remainder = n_temp % 10;
        reversed = reversed * 10 + remainder;
        n_temp /= 10;
    }
    
    printf("Given Number    : %d\n", n);
    printf("Reversed Number : %d\n", reversed);
    
    //check if given number equals reversed
    if (n == reversed) {
        printf("%d is a Palindrome.\n", n);
    } else {
        printf("%d is not a Palindrome.\n", n);
    }
}

Output

Enter a number : 14841
Given Number    : 14841
Reversed Number : 14841
14841 is a Palindrome.
Program ended with exit code: 0

Output

Enter a number : 1234
Given Number    : 1234
Reversed Number : 4321
1234 is not a Palindrome.
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to check if given number is a Palindrome or not using C program.