Computer Science Canada

Adding numbers binded as one interger

Author:  chrispaks [ Mon Apr 18, 2005 3:08 pm ]
Post subject:  Adding numbers binded as one interger

For example: if you entered 12345 into the get command area, the program will put "it is 5 numbers long and their sum is 15"

I know how to use the length command, but what command would I use to convert it into? Would I use a strint or intstr command? strintok? how would i seperate it so I can add the numbers?

Author:  Martin [ Mon Apr 18, 2005 3:11 pm ]
Post subject: 

code:
var number : string := "76391"
put length (number)
var sum : int := 0
for i : 1 .. length (number)
     put "number (", i, ") = ", number (i)
     sum = sum + strint (number (i))
end for

Author:  chrispaks [ Mon Apr 18, 2005 3:15 pm ]
Post subject: 

Thanks Razz
Now I'd just use the "put sum" to display it?

and the i is used as a variable to help add them together right?

Author:  Martin [ Mon Apr 18, 2005 3:18 pm ]
Post subject: 

Are you familiar with for loops? i is simply a counter.

Author:  chrispaks [ Mon Apr 18, 2005 4:42 pm ]
Post subject: 

no im just familiar with 'loops' in general
ill go read a guide and learn it, I dont even know much GUI im a noob at this stuff

Author:  Martin [ Mon Apr 18, 2005 4:55 pm ]
Post subject: 

A for loop is a counted loop.

code:
for i : 1 .. 5
     put i, " "..
end for


is the equivalent of
code:
var i : int := 0
loop
     i := i + 1
     put i, " "..
     if i = 5 then
          exit
     end if
end loop


: