5 Replies - 464 Views - Last Post: 02 April 2013 - 08:18 PM Rate Topic: -----

#1 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1