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

U

un[real]

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 :)
 
L

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."
 
J

John Harrison

un said:
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
 
G

Gaijinco

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!
 
U

un[real]

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
 
G

Gaijinco

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;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top