Sum digits of an integer: Difference between revisions

Added XBasic
(Add Uxntal)
(Added XBasic)
Line 11:
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F sum_digits(=n, base)
V r = 0
Line 1,025 ⟶ 1,024:
70 GOTO 40
80 PRINT "ITS DIGIT SUM:",U
90 STOP</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>ENTER A NUMBER:-12321
<pre>
ITS DIGIT SUM: 9</pre>
ENTER A NUMBER:-12321
ITS DIGIT SUM: 9
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">EnableExplicit
EnableExplicit
 
Procedure.i SumDigits(Number.q, Base)
Line 1,059 ⟶ 1,054:
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
EndIf</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>
Line 1,100 ⟶ 1,093:
{{works with|QB64}}
{{works with|VB-DOS}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER)
DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER)
 
CLS
Line 1,120 ⟶ 1,112:
LOOP
SumDigits% = iSum
END FUNCTION</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>
Line 1,149 ⟶ 1,139:
print "fe base 16 : "; SumDigits(hexdec("FE"), 16)
print "f0e base 16 : "; SumDigits(hexdec("F0E"), 16)
end</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|TI-83 BASIC}}===
Line 1,216 ⟶ 1,203:
PRINT "f0e base 16 :"; SumDigits(3854, 16) !0xf0e
END</syntaxhighlight>
 
 
==={{header|Visual Basic}}===
 
This version checks that only valid digits for the indicated base are passed in, exiting otherwise.
 
Line 1,248 ⟶ 1,233:
Debug.Print sumDigits("2", 2)
End Sub</syntaxhighlight>
 
{{out}} (in the debug window):
<pre>
Line 1,257 ⟶ 1,241:
0
</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Sum digits of an integer"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION SumDigits% (Num AS INTEGERnumber, NBase AS INTEGERnBase)
 
FUNCTION Entry ()
PRINT "The sums of the digits are:"
PRINT
PRINT "1 base 10 : "; SumDigits(1, 10)
PRINT "1234 base 10 : "; SumDigits(1234, 10)
PRINT "fe base 16 : "; SumDigits(0xfe, 16)
PRINT "f0e base 16 : "; SumDigits(0xf0e, 16)
END FUNCTION
 
FUNCTION SumDigits (number, nBase)
IF number < 0 THEN number = -number
IF nBase < 2 THEN nBase = 2
sum = 0
DO WHILE number > 0
sum = sum + (number MOD nBase)
number = number / nBase
LOOP
RETURN sum
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasicvb">sub SumDigits(number, nBase)
if number < 0 then number = -number : fi
if nBase < 2 then nBase = 2 : fi
2,122

edits