Thread: Help

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

    Help

    Write a program that uses function isnumberpalindrome.Test your program on the following numbers 10,34,123454321,4444,5678765






    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    float isNumPalindrome(int x);
    int main()
    {
            int num;
            clrscr();
            cout<<"\t******WELCOME******\n\n";
            cout<<"1 means PALINDROME, 0 means not a PALINDROME\n";
            cout<<"Enter Number to check whether its a PALINDROME=";
    
            cin>>num;
            num=isNumPalindrome(num);
            cout<<num<<endl;
            getch();
            return 0;
    }
            float isNumPalindrome(int x)
    {
            int pwr=0;
    
    	if (x<10)
    	return 1;
    
    
    	else
    {
    	while (x/(pow(10,pwr)) >=10)
    	pwr=pwr+1;
    
    	while (x>=10)
    {
    	if (x/pow(10,pwr) != (x % 10))
    	return 0;
    
    
    	else
    {	x = x / pow(10,pwr);
    	x = x % 10;
    	pwr = pwr-2;
    
    }
    }
    	return 1;
    }
    }
    [FONT="Arial Narrow"][/FONT]When i write a number greater than 10 the function does not return value 1(means no. is palindrome).
    I gues there is a logical error em stuck in it..kindly help..@@!!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    0) Use indentation to show which blocks belong in loops, which else matches which if etc.

    Code:
    float isNumPalindrome(int x)
    {
        int pwr=0;
    
        if (x<10) //what about negative values?
            return 1;
        else
        {
            while (x/(pow(10.0,pwr)) >=10)
                pwr=pwr+1;
    
            while (x>=10)
            {
                cout << x << ' ' << pwr << '\n'; //you could use debug output, to see what's going on
                if (x/pow(10.0,pwr) != (x % 10))
                    return 0;
                else
                {
                    x = x / pow(10.0,pwr);
                    x = x % 10;
                    pwr = pwr-2;
                }
            }
        return 1;
        }
    }
    1)
    Code:
    if (x/pow(10.0,pwr) != (x % 10))
    The left-hand side is a floating point value, the right-hand side is integral. E.g the number is 434. You will be comparing 4.34 against 4.0. They will always be unequal.

    2)
    Code:
                else
                {
                    x = x / pow(10.0,pwr);
                    x = x % 10;
                    pwr = pwr-2;
                }
    This section practically destroys the number entirely. E.g input was 146371. Assuming the else branch could be selected at first iteration (as it should), what would be left of x? 1?

    What you probably want to do is to chop off the first and the last digit. I suppose subtraction would be useful for the first, e.g 5675 - 5 * 1000 = 675. Dividing by 10 removes the last digit.

    --------

    The whole thing would probably work out better if you avoided the pow function to keep it entirely to integer arithmetics. You can calculate the power of ten by iteratively multiplying by 10. You can use some_power_of_ten /= 100; instead of pwr = pwr-2;
    Last edited by anon; 05-01-2009 at 07:27 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several "interesting" aspects of your code.
    1. You are using obsolete include <iostream.h>
    2. You are assigning integers from double (pow() returns a double).
    3. If the code looks about the same posted here as it does your end, you need to find out how to do indentation.

    But your real problem is that the code is comparing a floating point value with an integer:
    Code:
    	    if ((x/pow(10,pwr)) != (x % 10))
    The red bit produces a floating point value - if x is 121, then the result of that calculation will be 1.21 which is not equal to 1 - the value that x % 10 would give.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1. You are using obsolete include <iostream.h>
    I didn't bother to comment on that but this implies that you are probably also using an obsolete compiler...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed