By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,379 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,379 IT Pros & Developers. It's quick & easy.

I need help calculating the sum of the numbers of an integer

P: n/a
Like I said in the title, I have to calculate the sum of the numbers of
an interger with a recursive fonction.

Exemple: n=1255, sum= 1 + 2 + 5 + 5= 13

Do I have to use a string on can I use a simple cin ??

Thanks for your help !!

And sorry for my english :)

Nov 2 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
lpw
> Like I said in the title, I have to calculate the sum of the numbers of
an interger with a recursive fonction.

Exemple: n=1255, sum= 1 + 2 + 5 + 5= 13

Do I have to use a string on can I use a simple cin ??

Thanks for your help !!

And sorry for my english :)


Funny... this arrived right after Shiva's post, which, among other things,
states:

"As a rule, posters in comp.lang.c++ will not do homework, but will give
helpful hints if you have shown some willingness to try a solution."
Nov 2 '05 #2

P: n/a
un[real] wrote:
Like I said in the title, I have to calculate the sum of the numbers of
an interger with a recursive fonction.

Exemple: n=1255, sum= 1 + 2 + 5 + 5= 13

Do I have to use a string on can I use a simple cin ??


You can use 'simple cin'.

The key is understanding how % and / work on integers.

john
Nov 2 '05 #3

P: n/a
I would said that you really have to understand what n%10 and n/10 does
IF n is an integer. Experiment a bit (you can even combine them so also
try n/10%10 )

Now go do your homework!

Nov 2 '05 #4

P: n/a
Ok thanks... i'll work on that

Nov 2 '05 #5

P: n/a
I know it's my first time posting here, and I'm not asking for a
solution at all, I'm just looking for some hint about how to do it...

I can calculate the sum of the numbers but not in a recursive way, that
is why I posted here

Nov 2 '05 #6

P: n/a
This is completely off topic from this groups, but now that we are it:

OK, now that you are trying let me give you my idea, you can compare
and learn from it:

When you are doing recursion you find where the recursion is at, and
what's your base case.

So think that sum of all digits of a given number is the sum of the
last digit plus the sum of all the others digits, but the some of all
the others digits is the sum of the last digit plus... ˇYou have
recursion!

Ok, so let's translate into code what exactly means that precious
sentences:

The last digit of a given integer number is calculated with %10, it's
not God-sent either, the a%b returns the reminder of a/b, and a little
bit of math would give you that the reminder of a/10 is always the last
digit of it.

Now the funny thing is n/10. If n is an integer, n/10 drops the decimal
part of it because an integer doesn't hold decimal numbers.

So if you have a function sumDigits() the sentence "the sum of all
other digits (of a given number n)" means sumDigits(n/10)

And of course your base case, the thing that stops the recursion going
forever, is when you have a number that is single-digit. If that's the
case well the sum is the number itself:

Now HOCUS-POCUS:

int sumDigits(int n){

if(n<10)
return n;
else
return n%10 + sumDigits(n/10);

}

I thinks it's also worth to look at a non-recursive function to do
that:

int sumDigits(int n){

int sum;
for(sum=0; n>0; sum+=n%10, n/=10);

return sum;

}

Nov 3 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.