# Function;palindrome.

Page 1 of 1

## 5 Replies - 359 Views - Last Post: 02 April 2013 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317137&amp;s=0fae9e16c53ed1dafe3edc512bf35835&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DanielKostinskiy

Reputation: 0
• Posts: 14
• Joined: 10-December 12

# 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.

```#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;
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Function;palindrome.

### #2 mojo666

Reputation: 307
• Posts: 678
• Joined: 27-June 09

## 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 blackcompe

• D.I.C Lover

Reputation: 1114
• Posts: 2,425
• Joined: 05-May 05

## 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 DanielKostinskiy

Reputation: 0
• Posts: 14
• Joined: 10-December 12

## 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?

### #5 jon.kiparsky

• Pancakes!

Reputation: 6729
• Posts: 10,985
• Joined: 19-March 11

## 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.

### #6 mojo666

Reputation: 307
• Posts: 678
• Joined: 27-June 09

## 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.