Check The Programming Section
A palindromic number (also known as a numeral palindrome or a numeric palindrome) is a number (such as 16461) that remains the same when its digits are reversed. In other words, it has reflectional symmetry across a vertical axis. The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed.
The first 30 palindromic numbers (in decimal) are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202.
To implement this program I have used two user defined function as
getNoOfDigits() - will count how many digits are present in the number
int reverseNumber() - will reverse the number
Two variables num and count is used and declared as global because these variables are shared among the multiple functions. Inside the getNoOfDigits function count is incremented till the value of temp is greater than 0. At each iterations the value temp get updated as it is divided by 10.
#include<stdio.h>
#include<math.h>
int num, count=0;
void getNoOfDigits(){
int temp = num;
while (temp>0){
count++;
temp = temp/10;
}
}
int reverseNumber(){
int val = 0;
int temp = num;
while(count>0){
val = val + (pow(10,count-1))*(temp%10);
temp = temp/10;
count--;
}
return val;
}
int main(){
do{
fflush(stdin);
printf("Enter a number::");
scanf("%d",&num);
getNoOfDigits();
int val = reverseNumber();
if(val==num) printf("%d is a Plaindrome Number",num);
else printf("%d is not a palindrome Number",num);
printf("\nDo you want to continue Y/N:");
}while(getchar()=='Y'||getchar()=='y');
return 0;
}
Inside the reverseNumber() function, pow() is used to get the 10^n-th place. Such as if num is taken as 121, then 121 can be written as (10^2 * 1) + (10^1 * 2) + 10^0*1. So at each iteration we are getting the particular 10^n-th place and this step is repeated till the value of count is > 0.
At First Iteration:
(10^count-1) * (temp%10);
(10^3-1) * (121%10)
100 * 1
100
At Second Iteration:
(10^count-1) * (temp%10);
(10^2-1)* (12%10)
10*2
20
At Third Iteration:
(10^count-1)* (temp%10);
(10^1-1) *(1%10);
1*1
1
References:
https://en.wikipedia.org/wiki/Palindromic_number