hi i am a new member of daniweb and i have problem
i wrote a program in c that finds the numbers which are like that
abc...=pow(a,3)+pow(b,3)+pow(c,3)...
there are 5 number like that these are 1,153,370,371,407
but my program doesnt show 371

``````#include<stdio.h>
#include<math.h>
int
main ()
{
int c,d,k;
double i,b;

for(i=1;i<1000;i++){
k=0;
for(c=5;c>=0;c--){
b=floor(i/pow(10,c));
d=10*(i/pow(10,c)-b);
if(d!=0){k=k+(d*d*d);}
}
if(i==k){printf("i %.0f \n",i);}}
system("pause");
return 0;
}``````

> You cannot use C to solve this problem.
Of course you can, it just takes an additional library to help you do all the hard stuff.
http://gmplib.org/

Or if you're only interested …

> You cannot use C to solve this problem.
Of course you can, it just takes an additional library to help you do all the hard stuff.
http://gmplib.org/

Or if you're only interested in …

It is fine to use floats and doubles as loop indecies.

``````#include <stdio.h>

int main()
{
float y;

for(y = .1; y != 1.0; y += .1)
printf("%f\n", y);

return(0);
}``````

Are you saying this code is OK then?.

If you mean `for (int i=0; ...)`

You cannot use C to solve this problem. The highest integer value it can use is: unsigned long (32 bits) goes from 0 to 4,294,967,295 max.

There are other languages that can handle very large integers easily.

not really necessary.
both 999 and 3*9*9*9 (max possible values)
are <= 1000.

And I guess so are the people who have written that wonderful article on why 'floating point indices be avoided'.... ;)

Its the same as saying use system("pause") to pause the output screen -- though it works, its one thing that should not be used.

That's exactly my feeling. Float …

## All 21 Replies

>double i,b;

Don't use a float or double for index in a loop. Use a integer if you want
correct results.

>double i,b;

Don't use a float or double for index in a loop. Use a integer if you want
correct results.

You cannot use C to solve this problem. The highest integer value it can use is: unsigned long (32 bits) goes from 0 to 4,294,967,295 max.

There are other languages that can handle very large integers easily.

> You cannot use C to solve this problem.
Of course you can, it just takes an additional library to help you do all the hard stuff.
http://gmplib.org/

Or if you're only interested in a couple of math ops, then you could work out how to do long arithmetic in your own code instead.

commented: Thanks -Aia +1

thank u it will be useful.

> You cannot use C to solve this problem.
Of course you can, it just takes an additional library to help you do all the hard stuff.
http://gmplib.org/

Or if you're only interested in a couple of math ops, then you could work out how to do long arithmetic in your own code instead.

``````#include<stdio.h>

int value_digits( int a, int b, int c ) { return a*100 + b*10 + c ; }
int value_sum_power( int a, int b, int c ) { return a*a*a + b*b*b + c*c*c ; }

int main()
{
for( int a=0 ; a<10 ; ++a )
for( int b=0 ; b<10 ; ++b )
for( int c=0 ; c<10 ; ++c )
{
int number = value_digits(a,b,c) ;
if( number == value_sum_power(a,b,c) )
printf( "%d\n", number ) ;
}
return 0 ;
}``````
commented: Great job -Aia- +1
``````#include<stdio.h>

int value_digits( int a, int b, int c ) { return a*100 + b*10 + c ; }
int value_sum_power( int a, int b, int c ) { return a*a*a + b*b*b + c*c*c ; }

int main()
{
for( int a=0 ; a<10 ; ++a )
for( int b=0 ; b<10 ; ++b )
for( int c=0 ; c<10 ; ++c )
{
int number = value_digits(a,b,c) ;
if( number == value_sum_power(a,b,c) )
printf( "%d\n", number ) ;
}
return 0 ;
}``````

declaring the integers inside the for loop doesn't work in all the compilers. It's outside of the C99 model.

declare it at the beginning of the main function.

very true. whenever i try to write C,
a bit of it comes out as C++

i understood now

very true. whenever i try to write C,
a bit of it comes out as C++

You did a great job, one day I'll learn what C++ is all above. :)

You cannot use C to solve this problem. The highest integer value it can use is: unsigned long (32 bits) goes from 0 to 4,294,967,295 max.

There are other languages that can handle very large integers easily.

I think most compilers support 64-bit integers nowdays (long long or __int64)

 This works, which is what Vijayan posted. All I did was replaced int with longlong (VC++ 2005 used __int64) [/edit]

``````#include<stdio.h>
#include<math.h>
typedef __int64 LONGLONG;

LONGLONG value_digits( LONGLONG a, LONGLONG b, LONGLONG c ) { return a*100 + b*10 + c ; }
LONGLONG value_sum_power( LONGLONG a, LONGLONG b, LONGLONG c ) { return a*a*a + b*b*b + c*c*c ; }

int main()
{
LONGLONG a,b,c,number;
for( a=0 ; a<10 ; ++a )
{
for( b=0 ; b<10 ; ++b )
{
for( c=0 ; c<10 ; ++c )
{
number = value_digits(a,b,c) ;
if( number == value_sum_power(a,b,c) )
printf( "%I64d\n", number ) ;
}
}
}

return 0 ;
}``````

>double i,b;

Don't use a float or double for index in a loop. Use a integer if you want
correct results.

It is fine to use floats and doubles as loop indecies.

declaring the integers inside the for loop doesn't work in all the compilers. It's outside of the C99 model.

declare it at the beginning of the main function.

If you mean `for (int i=0; ...)` yes
But if you mean to include the `int number = ...` after the FOR statement, not true.

It is fine to use floats and doubles as loop indecies.

``````#include <stdio.h>

int main()
{
float y;

for(y = .1; y != 1.0; y += .1)
printf("%f\n", y);

return(0);
}``````

Are you saying this code is OK then?.

If you mean `for (int i=0; ...)` yes
But if you mean to include the `int number = ...` after the FOR statement, not true.

I thought it was clear on what I was talking about:

``````for( int a=0 ; a<10 ; ++a )
for( int b=0 ; b<10 ; ++b )
for( int c=0 ; c<10 ; ++c )``````

It is fine to use floats and doubles as loop indecies.

Nope, won't do. Try using double and float as your array indices while writing normal programs and you would know the reason. Comparision operators don't cut well with floating point numbes. Read this and this.

``````#include <stdio.h>

int main()
{
float y;

for(y = .1; y != 1.0; y += .1)
printf("%f\n", y);

return(0);
}``````

Are you saying this code is OK then?.

Not quite. Because of the inaccuracy of float and double your comparison can't be == and !=.
Use `for(y = .1; y < 1.0; y += .1)` instead.

Nope, won't do. Try using double and float as your array indices while writing normal programs and you would know the reason. Comparision operators don't cut well with floating point numbes.

Will so. The inaccuracies of float and double is a standard part of using floating point values, and knowledge of programming. You must always take into account this inaccuracy and program accordingly.

The above works fine with the stated change. For more accuracy, though, a small fudge factor can be added to the comparison to prevent .9999999 from inaccurately being displayed: `for(y = .1; y < 0.99; y += .1)` Armed with this knowledge, floating point is not the demon it's made out to be.

*shrugs* You have been warned... :D

*shrugs* You have been warned... :D

And you have been brainwashed :p

And I guess so are the people who have written that wonderful article on why 'floating point indices be avoided'.... ;)

Its the same as saying use system("pause") to pause the output screen -- though it works, its one thing that should not be used.

This works, which is what Vijayan posted. All I did was replaced int with longlong (VC++ 2005 used __int64)

not really necessary.
both 999 and 3*9*9*9 (max possible values)
are <= 1000.
int wouls suffice

You cannot use C to solve this problem. The highest integer value it can use is: unsigned long (32 bits) goes from 0 to 4,294,967,295 max.

There are other languages that can handle very large integers easily.

not really necessary.
both 999 and 3*9*9*9 (max possible values)
are <= 1000.
int wouls suffice

yes, it doesn't even get close to maxing out the largest int value :eek:

And I guess so are the people who have written that wonderful article on why 'floating point indices be avoided'.... ;)

Its the same as saying use system("pause") to pause the output screen -- though it works, its one thing that should not be used.

That's exactly my feeling. Float works some times. But why to used them when it is so problematic. Float works only when they try to imitate an integer. Why not to use the real thing then?. By virtue of
how a float is handled internally, is much faster also to use an integer.
And then do the calculation for the float inside the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.