by the way, it's not sure that the number will be over 10 so there is surely a if
thanks once again

03-29-2007

Brad0407

I'm not sure your math background but you could check the number of digits with log(|number|). As far as solving your overall problem, I think the easiest(most straightforward) way would be to convert the numbers to a string and then add each character of the string to the total. You can do that or do some pure math with something like:

Code:

// this will add all the digits in number
int digit_sum = 0;
for (int i = 0; i <= (int)log(abs(number)); i++)
digit_sum += (int)(abs(number) / pow(10, i)) &#37; 10;

Code:

(int)log(abs(number))

this returns the number of digits in the number

Code:

(int)(abs(number) / pow(10, i)) % 10)) % 10

This singles out the ith digit. For example, in the number 486, 6 is the 0th digit, 8 is the 1st digit, 4 is the 2nd digit.