My first challenge to you is to write a program that'll split a whole number of up to 15 digits into single digits that hold each place value. For example:
5918525 would return 5, 9, 1, 8, 5, 2, and 5.
However, you may only achieve this by using mathematical functions! Good luck! I'll be on my ol' DOS computer trying to do it too
.
Antoni Gual's doesn't work when given a zero, better it's still better than mine.
I always thought you'd have to use an array/stack to start at the one's place and still print them in order - nice work!
Well, anyway, here's mine:
Code:
DEFLNG A-Z
INPUT "Enter a non-negative integer: ", n
divisor = 1000000000 ' start at the billions place
' LONGs cannot have any more digits than this
PRINT "Here are the digits, in order:";
' This flag will be used to clip off leading zeros:
havePrintedANonZeroDigit = 0
DO UNTIL divisor = 1
digit% = (n \ divisor) MOD 10
divisor = divisor \ 10
IF (digit% <> 0) OR havePrintedANonZeroDigit THEN
PRINT digit%; ",";
havePrintedANonZeroDigit = -1
END IF
LOOP
' This ensures that for n = 0, a zero will be printed:
PRINT n MOD 10 ' Print the last digit (in the one's place)
END
Yea, I realized that the algorithms I wrote won't work with 15 digits... so 10 has to work
. Well, here's my code... it uses MOD and such, probably not as good as you guys though...
Code:
DECLARE SUB isolate (num)
DIM SHARED dig(10)
COMMON SHARED length
CLS
DO
INPUT "What number (1 - 9999999999) to isolate"; num
LOOP UNTIL num > 0 AND num < 9999999999
isolate (num)
FOR x = 1 TO 10
IF length >= x THEN PRINT dig(x);
NEXT x
SUB isolate (num)
FOR x = 1 TO 10
IF num < (10 ^ x) THEN length = x: GOTO lengthfound
NEXT x
lengthfound:
dig(1) = INT(num / (10 ^ (length - 1)))
FOR x = 2 TO length
dig(x) = num MOD 10 ^ (length - (x - 1))
dig(x) = INT(dig(x) / (10 ^ (length - x)))
NEXT x
END SUB
You guys and your fancy short codes which do all that mine do yet are much simpler :wink:.
Here ya go. I see this is basically Antoni's algorithm, but I'll post it anyways since it's a bit shorter and allows for input of 0.
Code:
DECLARE SUB split (n&)
INPUT n&
IF n& = 0 THEN PRINT 0 ELSE split n&
SUB split (n&)
IF n& THEN split n& \ 10 ELSE EXIT SUB
PRINT n& MOD 10
END SUB
*peace*
Meg.
That's what i meant :oops:
Your code is the best, as it happens always when you decide to show up... :lol:
title reiterated.
*peace*
Meg.
I need to learn how to do recursion effectively