Palindrome number in c is to check whether the number is palindrome or not.
In this tutorial we are going to see what is palindrome number, How to check that number is palindrome number in c.
What is Palindrome number ?
Any number which remains same when we reverse it.
for example : 121, 212, 12321
as we see if we reverse these numbers still all numbers remains same.
Logic to check Palindrome number in c :
For this first we take a number from user. we reverse the given number. after that we compare both given and reversed number if both are equal then number is palindrome number otherwise not.
number = 121
reverse number = 121
Number is palindrome.
Concept you should know from c :
- Declare variables
- Take number from user
- Reverse the given number
- Compare both numbers
- if both equal then print palindrome number
Program to check palindrome number in c
int n, reverse = 0, t;
printf("Enter a number to check if it is a palindrome or not\n");
t = n;
while (t != 0)
reverse = reverse * 10;
reverse = reverse + t%10;
t = t/10;
if (n == reverse)
printf("%d is a palindrome number.\n", n);
printf("%d isn't a palindrome number.\n", n);
- First include preprocessor directives stdio.h in program.
- Next write main function from where the execution of the program begins.
- Declare the variables int n, reverse = 0, t; n => to store number user enters, reevverse => to store reverse of given number, t is temporary variable.
- next we copy the given number into temporary variable. t = n;
- Now we perform the reverse of the given number mathematically. for this we use while loop. while (t != 0) which is used to traverse the number till end.
- Now the main logic comes here
let the number ‘n’ be 321 and as 321>0, while loop gets executed
then x=321%10—>which is 1.
The rev for the first loop execution is rev=1.
- Now the number ‘n’ has become ’32’ and n>0, while loop executes for the 2nd time.
then x=32%10—>which is 2.
The rev when loop executed the second time is rev=12.
- Now the number ‘n’ has become ‘3’ and n>0, while loop executes for the 3rd time
then x=3%10—>which is 3.
The rev when loop executed the third time is rev=123.
- Now as the number of variable ‘n’ is 0 which is not n>0 then the loop terminates.Then the final reverse is ‘123’.
- Now compare reverse and given number.
- If both Re equal pint number is palindrome else not.