addnumerals&

by (Login MCalkins)
Moderator

I put

a = a + ASC(MID$(t, i, 1)) - &H30

but that could be replaced by

a = a + VAL(MID$(t, i, 1))

Regards,
Michael



'public domain, april 2011, michael calkins
DEFINT A-Z
DECLARE FUNCTION addnumerals& (num AS LONG)
DIM n AS LONG

CLS

n = 576

DO
n = addnumerals&(n)
PRINT n
LOOP UNTIL n <= 9
SYSTEM

'should work for inputs in range 0x0 to 0x7fffffff
FUNCTION addnumerals& (num AS LONG)
DIM a AS LONG
DIM i AS INTEGER
DIM t AS STRING
t = LTRIM$(STR$(num))
a = 0
FOR i = 1 TO LEN(t)
a = a + ASC(MID$(t, i, 1)) - &H30
NEXT i
addnumerals& = a
END FUNCTION

Posted on Apr 4, 2011, 2:22 PM

Respond to this message   

Return to Index


Response TitleAuthor and Date
integer division instead of using a string on Apr 6