
-----------------------------------
markEGz
Sun Mar 26, 2006 4:07 pm

Figure out the numbers
-----------------------------------
Hi sorry for the noob question but i am stuck on this one problem..

"ask the user for an integer". Output the number of digits in the interger.
Then output the sum of the digits. If i input 567 - i have 3 digits and also
get their sum."

I used the simple length command for the # of digits but how can i really
figure their sum out? i tried working my way using char so it tracks down
every number that is inputed in the get statement but couldnt figure it out
Please help me with some ideas, thanks!

-----------------------------------
Tony
Sun Mar 26, 2006 4:18 pm


-----------------------------------
math!

basically each digit is located powers of 10.

floor() rounds real values down.

-----------------------------------
chrispminis
Sun Mar 26, 2006 4:40 pm


-----------------------------------
Well, since you are using length, I'm assuming your reading the integer as a string so, you merely have to convert each digit to an integer with strint. Then add them together.

var str : string 
var num : int := 0 

get str
put "Length : ", length (str)
for i : 1 .. length (str)
    num += strint (str (i)) %strint converts the string into an integer
end for
put "Sum : ", num


Hope that's what you were looking for :)

-----------------------------------
cool dude
Sun Mar 26, 2006 5:19 pm


-----------------------------------
pretty nice chrispminis! i actually did this prog in 5 minutes approximately because i kinda did it the long way by just getting the remainders and seperating each digit i.e. first get the thousands digit by dividing by 1000 and so on... however mine was more code so i like your way better! :)

-----------------------------------
chrispminis
Sun Mar 26, 2006 6:42 pm


-----------------------------------
Yeah, I know what you mean. I would've posted that, but Tony already did and since he mentioned length, I figured he was doing it with strings first. I did what you did a while ago when I was supposed to be learning the mod function.

-----------------------------------
markEGz
Sun Mar 26, 2006 8:28 pm

rofl..
-----------------------------------
lmao, thanks alot i kind of went crazy and came up with this ^^^

                                  var chars : array char of boolean
                                  var numInput : char
                                  var numCount : int
                                  var numLength : int := 0
                                  var num : int := 0
                                  var exitloop := false
                                  
                                  
                                  put "Please input your number * put a space after your last digit "
                                  
                                  loop
                                  
                                    Input.KeyDown (chars)
                                  
                                    get numInput 
 
                                        if numInput >= '1' and numInput 