473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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
6 4067
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
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
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
Ok thanks... i'll work on that

Nov 2 '05 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Burnett-Hall | last post by:
I'm trying to move to using tableless page layouts, but I've come across what appears to be a bug in IE5's rendering that I can't find a way to overcome. The page has a sidebar to the left of...
4
by: bonehead | last post by:
Greetings, A friend tells me that he has not been able to work out an expression that calculates elapsed minutes from TimeA to TimeB. For example: TIMEA TIMEB ELAPASED...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
13
by: Supra | last post by:
how do i get 2 different number in same row? using random example: 4 2 5 6 7 4 1 7 9 8 3 3 <===== i want different number but not same as opposite 8 5
4
by: Mahesh S | last post by:
Hi I am writing a sql query (added below) to calulate percentage. Its returning an integer value whereas I would like to get a float value (with 2 decimal precision). The query is: ...
8
by: sajid | last post by:
The CSS 2.1 Specification describes how to sort a list of selectors in order of specificity, but it doesn't provide a method to calculate the specificity of a single selector in isolation. I've...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
4
by: sigkill9 | last post by:
I'm trying to create a script that will accept a user entered integer and find only one set of prime numbers that add up to that integer but need some help. The script is supposed to only accept...
1
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.