Sun of digits in a number

S

snow.carriers

Hello, I would like to know how to do this:
Basically when I input a number, such as 123, it should display 6
(1+2+3), and for 666 would be 18. I'm a bit stuck here...

I know it would involve a while loop along that can do this condition:

nd1=num/100%10;
nd2=num/10%10;
nd3=num%10;, etc

And some how add it up. Can anyone help me? Thanks.
 
V

Victor Bazarov

Hello, I would like to know how to do this:
Basically when I input a number, such as 123, it should display 6
(1+2+3), and for 666 would be 18. I'm a bit stuck here...

I know it would involve a while loop along that can do this condition:

nd1=num/100%10;
nd2=num/10%10;
nd3=num%10;, etc

And some how add it up. Can anyone help me? Thanks.

Well, if you do it only once:

nd = num % 10;
nn = num / 10;

what do you get? What if you take one of those results and keep doing
the same operation with it (instead of 'num')? How many times will you
get to do it? What numbers will you need to add and where?

V
 
R

Ron Natalie

Hello, I would like to know how to do this:
Basically when I input a number, such as 123, it should display 6
(1+2+3), and for 666 would be 18. I'm a bit stuck here...

I know it would involve a while loop along that can do this condition:

nd1=num/100%10;
nd2=num/10%10;
nd3=num%10;, etc

And some how add it up. Can anyone help me? Thanks.
Do it the other way around: take the modulo to get a digit,
then divide by ten, if not zero do it all again.

More than that, I will not offer. Your teacher wants you to do
your own homework.
 
S

snow.carriers

Who said it was homework? I'm doing this on my spare time because I
want to learn C++. I've already said what I remember, but just
forgetting how to do it. I know I can use a while loop, but I'm not
sure how to set it up after that. Please don't assume things.
 
J

Josh Mcfarlane

Who said it was homework? I'm doing this on my spare time because I
want to learn C++. I've already said what I remember, but just
forgetting how to do it. I know I can use a while loop, but I'm not
sure how to set it up after that. Please don't assume things.

However, this is more something that involves basic math principles
rather than learning C++. If you were learning C++ you could always
move onto a non-math based item to learn while loops and the come back
to it after you had the math concepts down.

Easiest way:
Do it on paper. Find the patterns. Turn the patterns into programming.

You are on the first step, once you finish the second step you can
proceed to the third.
 
V

Victor Bazarov

Who said it was homework? I'm doing this on my spare time because I
want to learn C++.

So, are you doing it for a project at work? No? Then it *is* home work.
> I've already said what I remember, but just
forgetting how to do it. I know I can use a while loop, but I'm not
sure how to set it up after that.

Set up what? After what?

while (somecondition) {
bodyoftheloop
}

You need to figure out what condition to write and what to put in the body
of the loop (what operation you need repeated).
> Please don't assume things.

OK, but just for our information, why not?

V
 
S

snow.carriers

I'm just your average joe. I know math (I'm taking calculus currently).
There's no reason to flame me, you all think I'm the typical guy who
demands the code. I even stated what I've got so far. I'm just getting
ahead of a later class trying to learn. I never specifically asked for
the code -- but I'm not the guy who quickly just c/ps it in. I'm
learning to, just like all of you have learned from the start. I've
told you how much I know anyways...

I'm pretty sure all of you asked questions just like this when you
began learning. And no, it's not homework -- it's a hobby.

Dictionary defines homework as:
"Work, such as schoolwork or piecework, that is done at home. "

So if I'm at the library doing this, I guess it counts as homework?
Please have some logic towards your answer. I admire you for helping me
at the start though.
 
J

Josh Mcfarlane

I'm pretty sure all of you asked questions just like this when you
began learning. And no, it's not homework -- it's a hobby.

Actually, the question I would have asked would have been the
following:

What's the operator to find the remainder of the a number divided by
another?
What's the operator to add a number to itself?

Asking how defeats the purpose of learning. You may have questions
about the name of a function to do "foo", but asking how to do a
general algorithm is the same as asking for the algorithm itself.

If you have a number, 12353, and you want to add the digits, how do you
do that?

Well, you add 1 + 2 + 3 + 5 + 3.

How do you get the 3? Well, 12353 % 10 gives you 3. How do you get the
5? Well, 1235 % 10 gives you the 5.

How do you turn that into code? Patterns and repeatitions.
 
K

Kai-Uwe Bux

Victor said:
So, are you doing it for a project at work? No? Then it *is* home work.

You are using a definition of home work that (a) you won't find in
dictionaries and that (b) was clearly not the intended meaning of the posts
above starting with "Your teacher wants you to do your own homework."

[snip]
OK, but just for our information, why not?

Well, you are running an increased risk of patronizing people who do not
strictly deserve it.

On the other hand, you have a point: a news group post usually is very
little to go on, and you just have to make decisions on how to respond.


Best

Kai-Uwe Bux
 
S

snow.carriers

^
I know the logic behind it. I'm stuck implementing how to add the
1+2+3+4+5. I know you use the mod to get the digits, but how can you
add them while looping? And adding the variables while they are at it?
I got how to get the number of digits:

while(dig > 0){
count++;
dig = dig/10; }

Kinda stuck there.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Who said it was homework? I'm doing this on my spare time because I
want to learn C++.

Then is self-assigned homework, not very different.
 
J

Jay Nabonne

^
I know the logic behind it. I'm stuck implementing how to add the
1+2+3+4+5. I know you use the mod to get the digits, but how can you
add them while looping? And adding the variables while they are at it?
I got how to get the number of digits:

while(dig > 0){
count++;
dig = dig/10; }

Kinda stuck there.

So how do you add numbers in your head? You don't instantly grok the
entire sum. First you start out with nothing (0). Then you see the 1. Aha!
0 + 1 is 1. Then you see the 2. 1 + 2 is 3. Then you see three. 3 + 3 = 6.
Then you see the four -> 6 + 4 = 10. You *accumulate* the answer...

- Jay
 
J

John Harrison

^
I know the logic behind it. I'm stuck implementing how to add the
1+2+3+4+5. I know you use the mod to get the digits, but how can you
add them while looping? And adding the variables while they are at it?
I got how to get the number of digits:

while(dig > 0){
count++;
dig = dig/10; }

Kinda stuck there.

You get the rightmost digit with

dig%10

You add things up with +=

so

total += dig%10;

Put that in the right place in your loop. And drop count++, what does
that do?

john
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Kai-Uwe Bux said:
dictionaries and that (b) was clearly not the intended meaning of the
posts above starting with "Your teacher wants you to do your own
homework."

Many people use the expression: "I teached myself". In that case, maybe the
teacher does not know what he wants.
 
S

snow.carriers

Thanks for the obvious.
Anyways this is what I got so far, however for some reason only the
last digit displays.
while (dig > 0) {
dig = dig%10;
total += dig%10;
dig = dig/10;
}
 
J

Josh Mcfarlane

^
I know the logic behind it. I'm stuck implementing how to add the
1+2+3+4+5. I know you use the mod to get the digits, but how can you
add them while looping? And adding the variables while they are at it?
I got how to get the number of digits:

while(dig > 0){
count++;
dig = dig/10; }

Kinda stuck there.

Well, loop one you have sum = 1, loop two you have sum = 1+2. Break it
down you have sum as a moving total with the new mod number as the new
value.
 
J

John Harrison

Thanks for the obvious.
Anyways this is what I got so far, however for some reason only the
last digit displays.
while (dig > 0) {
dig = dig%10;
total += dig%10;
dig = dig/10;
}

Lets trace that out, starting with dig = 123, total = 0

dig = dig%10, so now dig = 3
total = dig%10, so now total = 3
dig = dig/10, so now dig = 0
so now the loop exits.

Do you see why it doesn't work? Do you see the mistake? It's hard to
advise because it's hard to know why you thought what you wrote would
work in the first place.

john
 
J

Josh Mcfarlane

Thanks for the obvious.
Anyways this is what I got so far, however for some reason only the
last digit displays.
while (dig > 0) {
dig = dig%10;
total += dig%10;
dig = dig/10;
}

What is up with the first line?

Say Digit = 1345

Digit = 1345 % 10 means digit now equals 5.
Total = += 5 % 10, which means total equals 5.
Dig = 5 / 10 which is 0, means Dig = 0 and loop breaks.
 
J

John Harrison

How would I do it so that
dig = dig/10
Would be equal to 12?

If dig = 123 before then you do

dig = dig/10

dig = 12 afterwards.

The problem with your loop is that dig does not equal 123 by the time
that you reach 'dig = dig/10'. Lets trace it again

start with dig = 123

while (dig > 0) - great this is true

dig = dig % 10 - before dig = 123, afterwards dig = 3 ***WRONG***

total += dig%10 - doesn't change dig

dig = dig / 10 - before dig = 3 (see ***WRONG***), so afterwards dig = 0

There's something very wrong here, do you understand assignment? Do you
understand that a program is a sequence of commands executed one after
another. At the moment you are just not getting it and it's hard to
explain such fundamental concepts.

If you are going to be a programmer you really need to be able to trace
out a four statement loop and be able to understand what it's behaviour is.

john
 

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

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top