
-----------------------------------
comp_help
Mon Apr 13, 2009 8:16 pm

turing - help w digits
-----------------------------------
What is it you are trying to achieve?
I am trying to output the number of digits entered in a number and the sum of those digits.

What is the problem you are having?
I don't know how to find the sum of the digits.


Describe what you have tried to solve this problem
I have solved the number of digits entered part of the problem, but it only works up to 6 digits.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Code is below:



var nm: int

put "Enter a number (integer):"
get nm

if nm div 100000 >= 1 and nm div 100000 < 2 then
put "\nEntered number has 6 digits"

elsif nm div 10000 >= 1 and nm div 10000 < 2 then
put "\nEntered number has 5 digits"

elsif nm div 1000 >= 1 and nm div 1000 < 2 then
put "\nEntered number has 4 digits"

elsif nm div 100 >= 1 and nm div 100 < 2 then
put "\nEntered number has 3 digits"

elsif nm div 10 >= 1 and nm div 10 < 2 then
put "\nEntered number has 2 digits"

elsif nm div 1 >= 0 and nm div 1 < 2 then
put "\nEntered number has 1 digits"

else
put "\nError: Number is greater than 6 digits."

end if



Please specify what version of Turing you are using
Turing 4.1.1

-----------------------------------
Tony
Mon Apr 13, 2009 8:31 pm

RE:turing - help w digits
-----------------------------------
[code]
div 10000
div 1000
div 100
div 10
div 1
[/code]
Notice a pattern?

-----------------------------------
andrew.
Mon Apr 13, 2009 8:34 pm

RE:turing - help w digits
-----------------------------------
See how use added a 0 for each if statement? Having 10^n is the same. You can use a for loop to go through different values of n. 


for n : 1..10 % The 10 is how many digits max you want
exit when nm div (10**n) >= 1 and nm div (10**n) < 2 % Remember to exit so that the program doesn't keep looping
if nm div 10**n >= 1 and nm div 10**n < 9 then 
put "\nEntered number has  "+ intstr(n+1) + " digits." 
end if
end for


Something like that should work. If you change the 10 to maxint, you will have an overflow, so you need some kind of exiting condition so that there is no overflow.

-----------------------------------
comp_help
Mon Apr 13, 2009 9:18 pm

RE:turing - help w digits
-----------------------------------
I would also like to know how to find the sum of the digits. :?

-----------------------------------
Tony
Mon Apr 13, 2009 10:09 pm

RE:turing - help w digits
-----------------------------------
For what you need to get the individual digits first. Worry bout summing them later.

if the input number is 123, how would you find the 2nd digit (in this case "2")?

-----------------------------------
comp_help
Mon Apr 13, 2009 10:39 pm

RE:turing - help w digits
-----------------------------------
I am guessing, but I think you would divide 123 by 10 then floor it. After that you can subtract ten. You can divide according to the number of digits. Can you tell me if this is right?

-----------------------------------
Tony
Mon Apr 13, 2009 10:50 pm

RE:turing - help w digits
-----------------------------------
As long as you know how to get ten in "subtract ten", then yes, this would work.

-----------------------------------
andrew.
Tue Apr 14, 2009 3:03 pm

RE:turing - help w digits
-----------------------------------
The cheap way out is to convert the int to a string and then just use length for the number of digits and use the substrings to find the digit. e.g. strNum (1) would be the last digit. This is the cheap way though and it's probably not what your teacher wants. I thought that you should know that you can convert ints to strings so that you can it in another assignment later.

-----------------------------------
saltpro15
Tue Apr 14, 2009 3:59 pm

RE:turing - help w digits
-----------------------------------
I suggest looking into div and mod
maybe this will work

div
mod

click those

-----------------------------------
saltpro15
Tue Apr 14, 2009 4:00 pm

RE:turing - help w digits
-----------------------------------
hmm, didn't work, well they'll be in the help index (F10)

-----------------------------------
Tony
Tue Apr 14, 2009 4:06 pm

Re: turing - help w digits
-----------------------------------
@saltpro

it's [tdoc]div[/tdoc]

-----------------------------------
saltpro15
Tue Apr 14, 2009 4:11 pm

RE:turing - help w digits
-----------------------------------
thanks Tony
[tdoc]
div
[/tdoc]
[tdoc]
mod
[/tdoc]

-----------------------------------
comp_help
Wed Apr 15, 2009 11:36 pm

RE:turing - help w digits
-----------------------------------
Ok, so for this code, in finding the sum of the digits, should I convert the input to string then get the part of the string according to the number of digits (ex. 2..3) and then convert it back to integer, and then finally add them? 
It is similar to what andrew was saying.

-----------------------------------
Tony
Thu Apr 16, 2009 12:33 am

RE:turing - help w digits
-----------------------------------
Except that taking a shortcut via strings pretty much defeats any purpose to this exercise. You really need to be comfortable with numbers.

You had the right idea for getting the individual digits. Now you just need to add them together in a sum variable.

-----------------------------------
andrew.
Thu Apr 16, 2009 4:23 pm

Re: RE:turing - help w digits
-----------------------------------
Ok, so for this code, in finding the sum of the digits, should I convert the input to string then get the part of the string according to the number of digits (ex. 2..3) and then convert it back to integer, and then finally add them? 
It is similar to what andrew was saying.I don't think you should do that. You're on the right track. I just told you how to do that because you will probably want to use something like that in the final ISU or something.
