# Thread: Exercise from my book.

1. ## Exercise from my book.

Hi, I found an exercise on my book like this:

"Exist 4 numbers, smallers than 500, that are special. If we take all his chars, but one after one, and elevates to the 3rd power, this will return the number. example:
1^3 + 5^3 + 3^3 = 153"

Now I need to make a program, to discover all numbers like this btw 0 and 500.

I dont know where to start, cuz the only way I found is making just on Loop, and than convert the number(index) into string, catch all the 3 chars of the string, that represents a single number and then transform then into Integer, apply the 3rd power on them, sum and than see if it is equals to the NUM.

But I think have another way, not? my way is so stupid I think.
I dont need you guys make my home-work, I just want to understand.

Thanks!!!

2. ## Ok, I found a way to solve this.

It's an stupid way but works
I'll be happy if someone could tell me a better way.

Thanks!

Code:
```/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/* Functions */
char subchar(char *string, int nIndex);
void check(int nNum);
int chatoi(char ch);

/* Global array, to hold the found  numbers */
int nNums[10];

/* main */
int main(void)
{
int nIndex; /* for looping */

/* start looping */
for(nIndex = 100; nIndex <= 500; ++nIndex)
check(nIndex); /* see if it's the kind of number */

/* pause */
system("PAUSE");

/* return 0 */
return 0;
}

char subchar(char *string, int nIndex)
{
return string[nIndex]; /* return the right char */
}

void check(int num)
{
char ch1, ch2, ch3, str[3];
int n1, n2, n3, curr = 0;

itoa(num,str,10); /* transform integer into string */
ch1 = subchar(str,0); /* catch first char */
ch2 = subchar(str,1); /* second char */
ch3 = subchar(str,2); /* third char */

/* transform into numbers */
n1 = chatoi(ch1);
n2 = chatoi(ch2);
n3 = chatoi(ch3);

/* check if the 3rd power of the numbers added together
results in the num */

if(((n1 * n1 * n1) + (n2 * n2 * n2) + (n3 * n3 * n3)) == num)
printf("Yes! %d\n",num);

}

int chatoi(char ch)
{
return ch - 48;
}```

3. Converting it to a string is unnecissary.

Just:
--------
divide the number by 100 to get the leftmost digit

divide the number by 10 then mod by 10 to get the middle digit

mod by 10 to get the rightmost digit

Then just continue as usual.
--------
Also, I can tell you tat 0 is one right off the bat 0^3 + 0^3 + 0^3 is 0

4. Code:
```#include<stdio.h>
#include<math.h>

int main()
{
int i;

for( i = 0; i <= 500; ++i )
if( pow( i / 100, 3 ) + pow( i / 10 % 10, 3 ) + pow( i % 10, 3 ) == i )
printf( "%d\n", i );

return 0;
}```

Note: it's 6 numbers, not 4

5. ## Ok, thanks man :)

Really thanks.

6. My two cents
Code:
```#include <stdio.h>
#include <math.h>

int main (void)
{
int     left, middle, right;

for (left = 0; left < 5; left++)
for (middle = 0; middle < 10; middle++)
for (right = 0; right < 10; right++)
{
if ((pow (left, 3) + pow (middle, 3) + pow (right, 3)) ==
left * 100 + middle * 10 + right)
printf ("%d%d%d\n", left, middle, right);
}
return 0;
}```
Is 4 3 digit numbers, which I assume is what you rbook wants. (the other 2, to make it are real cheap)