Author |
Message |
comp_help
|
Posted: Mon Apr 13, 2009 8:16 pm Post subject: 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:
Turing: |
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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Apr 13, 2009 8:31 pm Post subject: RE:turing - help w digits |
|
|
code: |
div 10000
div 1000
div 100
div 10
div 1
|
Notice a pattern? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Mon Apr 13, 2009 8:34 pm Post subject: 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.
Turing: |
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. |
|
|
|
|
![](images/spacer.gif) |
comp_help
|
Posted: Mon Apr 13, 2009 9:18 pm Post subject: RE:turing - help w digits |
|
|
I would also like to know how to find the sum of the digits. ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Apr 13, 2009 10:09 pm Post subject: 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")? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
comp_help
|
Posted: Mon Apr 13, 2009 10:39 pm Post subject: 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? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Apr 13, 2009 10:50 pm Post subject: RE:turing - help w digits |
|
|
As long as you know how to get ten in "subtract ten", then yes, this would work. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Tue Apr 14, 2009 3:03 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
saltpro15
![](http://compsci.ca/v3/uploads/user_avatars/9776171634ced6278d14c2.png)
|
Posted: Tue Apr 14, 2009 3:59 pm Post subject: RE:turing - help w digits |
|
|
I suggest looking into div and mod
maybe this will work
click those |
|
|
|
|
![](images/spacer.gif) |
saltpro15
![](http://compsci.ca/v3/uploads/user_avatars/9776171634ced6278d14c2.png)
|
Posted: Tue Apr 14, 2009 4:00 pm Post subject: RE:turing - help w digits |
|
|
hmm, didn't work, well they'll be in the help index (F10) |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
saltpro15
![](http://compsci.ca/v3/uploads/user_avatars/9776171634ced6278d14c2.png)
|
Posted: Tue Apr 14, 2009 4:11 pm Post subject: RE:turing - help w digits |
|
|
thanks Tony
div
mod
|
|
|
|
|
![](images/spacer.gif) |
comp_help
|
Posted: Wed Apr 15, 2009 11:36 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu Apr 16, 2009 12:33 am Post subject: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Thu Apr 16, 2009 4:23 pm Post subject: Re: RE:turing - help w digits |
|
|
comp_help @ Wed Apr 15, 2009 11:36 pm wrote: 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. |
|
|
|
|
![](images/spacer.gif) |
|