Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do you output the number and sum of the digits?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dc116




PostPosted: Fri Dec 12, 2008 9:00 pm   Post subject: How do you output the number and sum of the digits?

Ask the user for an integer. Output the number of digits in the integer. Then output the sum of the digits. (i.e. 1234 has 4 digits and their sum is 10)

Any help would be greatly appreciated. Smile
Sponsor
Sponsor
Sponsor
sponsor
The_Bean




PostPosted: Fri Dec 12, 2008 9:12 pm   Post subject: Re: How do you output the number and sum of the digits?

Make an attempt at it before asking a question about it, and show us what you have done so far.

You'll need to use:
Turing:
Dan




PostPosted: Fri Dec 12, 2008 9:15 pm   Post subject: RE:How do you output the number and sum of the digits?

Well i would do it by inputing the number as a string. Then the number of digits (assuming the input is vaild) is the length of the string.

Finding the sum is just a matter of looping threw the contents of the string and adding them to a counter.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
ecookman




PostPosted: Fri Dec 12, 2008 9:16 pm   Post subject: RE:How do you output the number and sum of the digits?

hehe as i was kinda saying before on the other thread...another thing to keep in mind when asking for help try it yourself (yes,yes i am not the most faithful a that either) saves you from people saying well what do you have done so far.



if you have no clue ask what would i need to do this or could someone start me off.

you are lucky that The_Bean did so usually you get the first line
dc116




PostPosted: Fri Dec 12, 2008 10:46 pm   Post subject: Re: How do you output the number and sum of the digits?

This is what I have so far.

code:

var number : string
var counter : int
counter := 0
put "Please enter an integer."
get number
put ""
put length(number), " digits"


I'm not sure how to use intstr() and strint() and counters to get the sum. Neutral
Can someone give me a hint on that part?
Tony




PostPosted: Fri Dec 12, 2008 11:01 pm   Post subject: RE:How do you output the number and sum of the digits?

given an integer, how would you get a digit in it's Nth place?

Here's an example: the number is 123. What is the digit in the 10s place? It's 2. Try to explain how you got to that answer though.

If you are simply reading "123" as a word, and taking a specific "letter" in the proper place (finding that place by counting the number of letters), then you'll find intstr() and strint() useful.

If you are using Math to isolate the digit, then div, mod and powers of 10 are helpful tools.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dc116




PostPosted: Sat Dec 13, 2008 9:20 pm   Post subject: Re: How do you output the number and sum of the digits?

Thanks guys! I finally figured it out.

code:

var a : int
var number : string
put "Please enter an integer."
get number
put ""
a:= length(number)
put a, " digits"
var n,p,counter1,counter2 : int
n := strint(number)
counter1 := n
counter2 := 0
for decreasing count: a..1
    p := counter1 div (10**(count-1))
    counter2 := counter2 + p
    counter1 := counter1 - (10**(count-1))*p
end for
put "The digits sum is ",counter2


Only problem is, it is insanely complicated and hard to understand. Any suggestions on how to improve (or shorten) it? Mr. Green
Tony




PostPosted: Sat Dec 13, 2008 9:25 pm   Post subject: RE:How do you output the number and sum of the digits?

Use better variable names. When I'm reading "a" or "p", I have no idea what they are.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
DanielG




PostPosted: Sat Dec 13, 2008 11:12 pm   Post subject: RE:How do you output the number and sum of the digits?

I believe a slightly more difficult version of this question was asked as the second question of the first dwite this year, you could look there for ideas on how to make yours better.
dc116




PostPosted: Sun Dec 14, 2008 6:16 pm   Post subject: Re: How do you output the number and sum of the digits?

Thanks for the help everyone. This is my final version.

code:

var digits : int
var number : string
put "Please enter an integer."
get number
put ""
digits := length(number)
put digits, " digits"
var digit_value,counter1,counter2 : int
counter1 := strint(number)
counter2 := 0
for decreasing count: digits..1
    digit_value := counter1 div (10**(count-1))
    counter2 := counter2 + digit_value
    counter1 := counter1 - (10**(count-1))*digit_value
end for
put "The digits sum is ",counter2
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: