C Program to check whether the number is a Palindrome

What is a Palindrome Number?

A number which remains the same when the digits are reversed is called a palindrome number. For example, 555, 10101, 45654 etc; are palindrome numbers. However, 123, 300 etc; are not.

The following is a C program to check whether the entered number is palindrome or not:

Expected Output:

1st run:

2nd run:

How it works

The following table demonstrates what happens at each iteration of the while loop, assuming num =  1551.

Iteration rem rev tmp_num
After 1st iteration rem=1551%10=1 rev=0*10+1=1 tmp_num=1551/10=155
After 2nd iteration rem=155%10=5 rev=1*10+5=15 tmp_num=155/10=15
After 3rd iteration rem=15%10=5 rev=15*10+5=155 tmp_num=15/10=1
After 4th iteration rem=1%10=1 rev=155*10+1=1551 tmp_num=15/10=0

Recommended Reading:

 

Leave a Comment

%d bloggers like this: