Rem Program tstmagic.bas
Rem Tests whether a given positive integer is a magic number
Rem A number is a magic number if the sum of digits of the number
Rem taken repeatedly is 1.
Rem For example - 12547 is a magic number
print "MAGIC NUMBER TESTER"
Do
  Input "Enter a positive integer:", n
  if n >= 0 and int(n) = n then exit do
  print "You have not entered a positive integer"
Loop
Do
   'Find the 'sum' of digits of 'n'
   gosub 1000
   'and set the number as 'sum' & repeat the procedure
   ' till the 'sum' is < 10 (i.e. has single digit)
   n = sum
Loop until n < 10
if n = 1 then
   print "A Magic Number"
else
   print "Not a magic number"
end if
end

Rem Subroutine to find the sum of digits of the number 'n'
1000:
sum = 0
while n > 0
  digit = n mod 10
  n = n \ 10
  sum = sum + digit
wend
Return
