Check whether a Number is Palindrome or Not: C Program

User inputs a integer number and we write a program to reverse that number and if the user input value and the reversed number are equal then its a palindrome number, if not, its not a palindrome number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program

Palindrome Number:
If a number remains same even if we reverse its digits then the number is known as palindrome number.

In other words, a number when read in both forward and backward way is same, then such a number is called a palindrome number.

Note: We assign variable reverse = 0 to avoid garbage values in variable reverse.

Reversing a Number Logic

If user enters 456, we apply the modulo division to get the individual values.
Ex:
456 % 10 = 6
45 % 10 = 5
4 % 10 = 4

After each iteration of while loop we add the reminder to the previous value of variable reverse. We also multiply the variable reverse with 10, to increment the decimal place.

We get 456, 45 and 4 by dividing the original value by 10.
Ex:
456 user entered value.
456 / 10 = 45
45 / 10 = 4

Now the reverse:

reverse = (reverse * 10 ) + rem;

06 = (0*10) + 6
65 = (6*10) + 5
654 = (65*10) + 4

So finally the reversed number is 654.

Check For Palindrome Logic

If the user entered value(original value) is equal to the revered number, then its palindrome number or else its not a palindrome.

For Example:
user entered number is 456.
Reversed number is 654.

So, 456 is not equal to 654, so the number 456 is not a palindrome.

Check whether a Number is Palindrome or Not: C Program



YouTube Link: https://www.youtube.com/watch?v=-zGyt48r5BU [Watch the Video In Full Screen.]


Source Code: Check whether a Number is Palindrome or Not: C Program

#include < stdio.h >

int main()
{
    int num, reverse = 0, rem, temp;

    printf("Enter a integer number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;
        reverse = reverse * 10 + rem;
        num     = num / 10;
    }

    printf("Reversed number is %d\n", reverse);

    if(temp == reverse)
    {
        printf("Entered number is palindrome\n");
    }
    else
    {
        printf("Entered number is not palindrome\n");
    }

    return 0;
}

Output 1:
Enter a integer number
12345
Reversed number is 54321
Entered number is not palindrome

Output 2:
Enter a integer number
123321
Reversed number is 123321
Entered number is palindrome

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert