C Program to Check Whether a Number is Palindrome or Not

The program check whether a number is palindrome or not by comparing original number and its reverse.

A palindrome number is a number that remains the same even after reversing it.

For example, 1001 and 12321. Picking from the example, 1001 remains the same even after we reverse it. Unlike 1021, which a non-palindrome as after reversing it we would get 1201 that is not equivalent to 1021.

whether a pallindrome or not

C Program to check whether a number is palindrome or not

Program:

/*
palindrome program in C
*/

#include<stdio.h> 
#include<conio.h> 
void main() {
 int number,remainder,temp,i,sum=0; 
clrscr();
 printf("\n Please enter a number = "); 
scanf("%d",&number);
 temp=number; 
while(temp!=0) {
 remainder=temp%10;
 sum=sum*10+remainder;
 temp=temp/10; 
} 
if(sum==number) 
printf("It is a palindrome"); else printf("It is not a palindrome"); 
getch();
 }

Output:

Please enter a number = 988
It is not a palindrome

Explanation:

1) Getting input from the user
2) Storing it to a temporary variable ‘temp’
3) While temp is not equal to zero, do
4) Find the last digit from input by using modulo operator which would give remainder, for example, if we divide 1001 by 10, we would get 1 as remainder. In simple words, this how we are obtaining the last digit on each iteration

5) Adding the result with ‘sum'(initially 0 ) variable which is multiplied first with 10 on each iteration (Creating the reverse of that input)
6) Dividing ‘temp’ by 10 and also assigning it to ‘temp’
7) Repeat 3 to 6 till ‘temp’ is not equal to 0
8) Checking whether the final sum stored in ‘sum’ variable is equivalent to the input or not
9) If yes then it is a palindrome number

Leave a Reply