#include <iostream> #include <cmath> using namespace std; bool isNumPalindrome(int num); int main() {int numbers; //10, 34, 22, 333, 678, 67876, 44444, 123454321 cout<<"enter a number"<<endl; cin>>numbers; if (isNumPalindrome(numbers)) { cout << numbers << " is a Palindrome" << endl; } else { cout << numbers << " isn't a Palindrome" << endl; } } bool isNumPalindrome(int num) { int pwr = 0; if (num < 10) return true; else { while (num / static_cast<int>(pow(10.0, pwr)) >= 10) pwr++; while (num >= 10) { int tenTopwr = static_cast<int>(pow(10.0, pwr)); if ((num / tenTopwr) != (num % 10)) return false; else { num = num % tenTopwr; num = num / 10; pwr = pwr - 2; } }//end while return true; } }

# Function;palindrome.

Page 1 of 1## 5 Replies - 761 Views - Last Post: 02 April 2013 - 08:18 PM

### #1

# Function;palindrome.

Posted 29 March 2013 - 01:00 PM

Problems with the function. I typed in 90049 and it gave me is a palindrome. Any help would be awesome.

##
**Replies To:** Function;palindrome.

### #2

## Re: Function;palindrome.

Posted 29 March 2013 - 01:24 PM

When you remove the 9's, you are left with 004 which is treated as 4. Since it is less than 10 it is treated as a palindrome.

### #3

## Re: Function;palindrome.

Posted 29 March 2013 - 01:32 PM

If possible, you'd be better off treating the number as a string with the C function itoa() and going the easy route.

### #4

## Re: Function;palindrome.

Posted 31 March 2013 - 12:48 PM

How would I change it into a string?^

Can you show me how that would work?

Can you show me how that would work?

### #5

## Re: Function;palindrome.

Posted 31 March 2013 - 01:03 PM

String or an array of ints, either would make sense.

To convert it into an int array, you'd want to peel off digits from the back end using the mod and divs operator.

Another approach would be to just compare outer elements. Again, n % 10 gets you the ones place. The left-hand side takes a little math, but it's not difficult math.

In both cases, you'd want to get the length of the int. You can use the log to the base 10 to get this, but be careful or you'll get fencepost error at one edge.

To convert it into an int array, you'd want to peel off digits from the back end using the mod and divs operator.

Another approach would be to just compare outer elements. Again, n % 10 gets you the ones place. The left-hand side takes a little math, but it's not difficult math.

In both cases, you'd want to get the length of the int. You can use the log to the base 10 to get this, but be careful or you'll get fencepost error at one edge.

### #6

## Re: Function;palindrome.

Posted 02 April 2013 - 08:18 PM

Instead of going the string route, you can just alter your logic to account for the case when the outer elements are 0. This occurs when the number is less than 10^(pwr-1) and divisible by 10.

Page 1 of 1