407 missing when printing an Amstrong C number

I am using Code :: Block 10.02 and When print Amstrong Number in CI I expect this result:

153
370
371
407

But he only prints:

153
370
371

This is my code in C:

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

    int a,b,c;
    for  (a=1;a<=9;a++)
        for(b=0;b<=9;b++)
            for(c=0;c<=9;c++)
            {
                    if(pow(a,3)+pow(b,3)+pow(c,3)==100*a+10*b+c)
                           printf("\n%d%d%d",a,b,c);
            }

}

And this is my screen: http://daynhauhoc.com/uploads/default/3011/9598a2ad2183198f.png

But, when I use this code, it works very well:

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

    int a,b,c;
    for  (a=1;a<=9;a++)
        for(b=0;b<=9;b++)
            for(c=0;c<=9;c++)
            {
                    int d=pow(a,3)+pow(b,3)+pow(c,3);
                    int e=100*a+10*b+c;
                    if(d==e)
                           printf("\n%d%d%d",a,b,c);
            }

}

Will someone please tell me an explanation?

+4
source share
3 answers

Oh, I know why! You use pow(a,3)+pow(b,3)+pow(c,3)==100*a+10*b+cHowever, you use pow()return to determine if it has the same float and integer. Do not use or between integer and float!double====!=

+4
source

, pow () - . , pow (4, 3) 64, 64. 407 , , 407, .

pow (a, 3) a*a*a.

BTW. pow, . , , " 407", "- " , "- " , " 407" . , - " ", - " ".

BTW. int, . , 407.000000000000000001, 407, 406.999999999999999999, 406.

+3

This problem arises because it powis a double function, and for some time is not true, for example: 9999.99999 = 10e4.

The tip uses powfits value for an integer

0
source

All Articles