Like Tree1Likes
  • 1 Post By Dontgiveup

Number Palindrome

This is a discussion on Number Palindrome within the C++ Programming forums, part of the General Programming Boards category; Hallo, I would like to wrote a programme that has two bool functions, one for decimal and one for binary. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Number Palindrome

    Hallo, I would like to wrote a programme that has two bool functions, one for decimal and one for binary. The function is supposed to show whether the given parameter is a palindrome in decimal or binary system or not. Now here is the tricky part. No divisions and multiplications allowed.
    Piias Das likes this.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Convert it to a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Convert a number to string. Interesting. I don't really know how I can convert a number to a string. So I have 15, how can I convert it this to a string? I thought they were two separate data types that can't be mixed.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    In binary (haven't tested, just an idea):

    Code:
    inline bool getbit(unsigned x, unsigned bit)
    {
        // gets specified bit starting from 0
        return (x & (1 << bit)) != 0;
    }
    
    bool testbin(unsigned x)
    {
        // count number of visible digits
        unsigned n = 0;
        unsigned tempx = x;
        do
        {
            ++n;
            tempx >>= 1;
        }
        while (tempx > 0)
        
        // using previously computed number, test if palindrome
        for (unsigned i = 0; i < n >> 1; i++) // n >> 1 since n / 2 is not allowed
        {
            if (getbit(x, i) != getbit(x, n - i - 1))
                return false;
        }
        return true;
    }
    In decimal:

    depends whether modulo is also allowed.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks but you used division (i < n / 2) and no division is allowed.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    You can always implement division and multiplications with shifts. Just horribly inefficient when you have dedicated circuits in your CPU that can do them in hardware.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Code:
    bool IsPalindrome(int Input)
    {
    	char szInput[MAX_PATH] = {'0'};
    
    	//convert to string
    	_snprintf(szInput, MAX_PATH -1, "%d", Input);
    
    	//find length
    	int Len = lstrlen(szInput);
    
    	//for each character
    	for(int i=0;i<Len;i++)
    	{
    		//check to see if corresponding chars match
    		if(szInput[i] != szInput[Len-i])
    		{
    			//is not palindrome
    			return false;
    		}
    		if(i >= (Len - i) )//more than 1/2 way thru string and each char has matched, so is palindrome
    		{
    			return true;
    		}
    	}
    	return false;
    }
    PS I left a bug in the code.......
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Dontgiveup View Post
    Thanks but you used division (i < n / 2) and no division is allowed.
    I edited it a few seconds before you posted.
    I never put signature, but I decided to make an exception.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by novacain View Post
    Code:
    bool IsPalindrome(int Input)
    {
    	char szInput[MAX_PATH] = {'0'};
    
    	//convert to string
    	_snprintf(szInput, MAX_PATH -1, "%d", Input);
    
    	//find length
    	int Len = lstrlen(szInput);
    
    	//for each character
    	for(int i=0;i<Len;i++)
    	{
    		//check to see if corresponding chars match
    		if(szInput[i] != szInput[Len-i])
    		{
    			//is not palindrome
    			return false;
    		}
    		if(i >= (Len - i) )//more than 1/2 way thru string and each char has matched, so is palindrome
    		{
    			return true;
    		}
    	}
    	return false;
    }
    PS I left a bug in the code.......
    It is very likely that printf uses division to do the conversion.
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  2. next palindrome number
    By kris.c in forum C Programming
    Replies: 5
    Last Post: 09-05-2006, 05:43 AM
  3. palindrome
    By nightingale in forum C Programming
    Replies: 2
    Last Post: 07-26-2003, 02:45 PM
  4. Palindrome
    By Ginny Morgan in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 04:04 PM
  5. Palindrome
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21