473,322 Members | 1,620 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,322 software developers and data experts.

Adding digits

I am now making a program that adds the digits in a number, any positive
number anyway. How do I code the while loop to accomplish this? (example:
user enters the number 123456, and the program determines the sum of the
digits is 21)
Sep 10 '05 #1
8 1678
deanfamily11 wrote:
I am now making a program that adds the digits in a number, any positive
number anyway. How do I code the while loop to accomplish this? (example:
user enters the number 123456, and the program determines the sum of the
digits is 21)


Take a look at the modulous operator and think about powers of 10.

Homework Hint: Write down each step you would do by hand to achieve the
same result (sum of the digits) then attempt to turn each step into
programming.

Sep 10 '05 #2
deanfamily11 wrote:
I am now making a program that adds the digits in a number, any positive
number anyway. How do I code the while loop to accomplish this? (example:
user enters the number 123456, and the program determines the sum of the
digits is 21)


Here's a clue,

num % 10 == smallest digit of num

num / 10 == everything except the smallest difit of num

Put those two together in a loop and you can get the sum of digits.

john
Sep 10 '05 #3
M
On Sat, 10 Sep 2005 15:51:55 GMT, "deanfamily11"
<de**********@verizon.net> wrote:
I am now making a program that adds the digits in a number, any positive
number anyway. How do I code the while loop to accomplish this? (example:
user enters the number 123456, and the program determines the sum of the
digits is 21)


You can use modulo as already stated or you can input the number as a
string and use a loop to add each of the digits minus ascii 0.
Sep 10 '05 #4
On Sat, 10 Sep 2005 16:49:16 GMT, M <th********@usenet.net> wrote in
comp.lang.c++:
On Sat, 10 Sep 2005 15:51:55 GMT, "deanfamily11"
<de**********@verizon.net> wrote:
I am now making a program that adds the digits in a number, any positive
number anyway. How do I code the while loop to accomplish this? (example:
user enters the number 123456, and the program determines the sum of the
digits is 21)


You can use modulo as already stated or you can input the number as a
string and use a loop to add each of the digits minus ascii 0.


....which works on most implementations, since most implementations use
ASCII (not 'ascii', there is no such thing).

On the other hand you subtract '0' from any digit character and get
the correct result on every C++ implementation in existence.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 11 '05 #5
M
On Sun, 11 Sep 2005 00:49:23 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
...which works on most implementations, since most implementations use
ASCII (not 'ascii', there is no such thing).


What's your point? I didn't hold the shift key? Is everyone in this
group so intolerant?
Sep 12 '05 #6

M wrote:
On Sun, 11 Sep 2005 00:49:23 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
...which works on most implementations, since most implementations use
ASCII (not 'ascii', there is no such thing).


What's your point? I didn't hold the shift key? Is everyone in this
group so intolerant?


Do not snip the relevant part. Jacks answer was:
You can use modulo as already stated or you can input the number as a
string and use a loop to add each of the digits minus ascii 0.

...which works on most implementations, since most implementations use
ASCII (not 'ascii', there is no such thing).

On the other hand you subtract '0' from any digit character and get
the correct result on every C++ implementation in existence.


The last bit - what you snipped;-) - is the most relevant part. Read it
(both paragraphs) again and come back if you do not understand it.

/Peter

Sep 12 '05 #7
M
On 12 Sep 2005 05:41:11 -0700, pe***************@gmail.com wrote:
Do not snip the relevant part... The last bit - what you snipped;-) - is the most relevant part...


Relevant to his restatement of my answer, perhaps, but not relevant to
what I asked him.
Sep 13 '05 #8
M wrote:
On 12 Sep 2005 05:41:11 -0700, pe***************@gmail.com wrote:
Do not snip the relevant part...

The last bit - what you snipped;-) - is the most relevant part...


Relevant to his restatement of my answer, perhaps, but not relevant to
what I asked him.


His point was not all systems use ASCII.

Sep 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
I'm trying to learn this. My problem, I have 2 strings that represent floats. I want to add them together. Should I look at using Cint to convert each character in the string and add...
38
by: Astra | last post by:
Hi All Could somebody please confirm that if I change my JS expression test from: if (!(/^*$/.test(document.form1.fred.value))) to if (!(/^*$/.test(document.form1.fred.value)))
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
9
by: joanne matthews (RRes-Roth) | last post by:
I'm getting different results when I add up a list of floats depending on the order that I list the floats. For example, the following returns False: def check(): totalProp=0 inputs= for each...
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
4
by: Shisou | last post by:
hello everyone, Well I tried to solve this one on my own but it seems i need your help again :\ I'm trying to write a program to add or subtract two large integers... easy enough.. Where i...
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
1
by: lornab | last post by:
Good Afternoon I need a column to show in a report as 10 digits long by adding zero's to the left of the result. My select statement is like this: SELECT a.client, a.apar_id FROM...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
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.