A palindromic number

on Thursday, November 26, 2009


Write a code which will check whether the number is palindrome or not.PS: Don not convert a number into string .













SOLUTION

bool IsPalindrome( int n){
int a=n, b=0;
while( n > 0){
b = b*10 + n%10;
n /= 10;
}
if ( a == b)
return true;
return false;
}

4 comments:

Anonymous said...

reverse=0
temp=number
while(number > 0)
1.extract last digit using mod 10
2.divide number by 10
3.reverse=reverse * 10 + last digit

if temp==reverse
palindrome
else
not palindrome

Anonymous said...

now we know how to check the no. is palindrome or not,
lets solve this problem:

Given a no. which is palindrome, find the next palindrome.

For example input: 808
output: 818

hifun said...

@above:
Your approach is right.
For your question: go to the middle element increment it by 1. ( if digits are odd)
for even digits increment both middle digits by 1.
however, special care is to be taken if middle digit is 9.

Anonymous said...

hmmm, correct

Post a Comment