Check Whether a Number is Palindrome or Not in C

Exercise:

Write a C Program to check whether a number is palindrome or not.

Click Here to View the Solution!
#include <stdio.h>
int main() {
    int num, reversed = 0, remainder, original;
    printf("Enter an integer: ");
    scanf("%d", &num);
    original = num;

    // reversed integer is stored in reversed
    while (num != 0) {
        remainder = num % 10;
        reversed = reversed * 10 + remainder;
        num /= 10;
    }

    // palindrome if original and reversed are equal
    if (original == reversed)
        printf("%d is a palindrome.", original);
    else
        printf("%d is not a palindrome.", original);
    return 0;
}
Click Here to View the Output!
Enter an integer: 88988
88988 is a palindrome.
Click Here to View the Explanation!
  • This program is used to determine whether a number is a palindrome.
  • In main(), the program requests the user to enter an integer and stores it in a variable num using the scanf() function. The value of num is then stored in original variable.
  • A while loop is initialized which will continue until num != 0 is true.
  • The remainder of num is calculated by taking modulus of num with 10.
  • The reverse of num is calculated by using the formula reversed * 10 + remainder and stored in the variable reversed.
  • In each iteration, the value of num is reduced 10 times by dividing num by 10 which will be the value used in the next iteration.
  • After the loop exits, original and reversed are compared using an if statement. If (orginal will be equal to reversed), then the entered number will be a palindrome. Else, the message “original is not a palindrome” is printed.
  •  The return 0 statement is used to exit the program execution.
%d bloggers like this: