Sum digits of an integer: Difference between revisions

Content added Content deleted
(Add Uxntal)
(Added XBasic)
Line 11: Line 11:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Nim}}
{{trans|Nim}}

<syntaxhighlight lang="11l">F sum_digits(=n, base)
<syntaxhighlight lang="11l">F sum_digits(=n, base)
V r = 0
V r = 0
Line 1,025: Line 1,024:
70 GOTO 40
70 GOTO 40
80 PRINT "ITS DIGIT SUM:",U
80 PRINT "ITS DIGIT SUM:",U
90 STOP
90 STOP</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>ENTER A NUMBER:-12321
<pre>
ITS DIGIT SUM: 9</pre>
ENTER A NUMBER:-12321
ITS DIGIT SUM: 9
</pre>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">
<syntaxhighlight lang="purebasic">EnableExplicit
EnableExplicit


Procedure.i SumDigits(Number.q, Base)
Procedure.i SumDigits(Number.q, Base)
Line 1,059: Line 1,054:
Repeat: Delay(10) : Until Inkey() <> ""
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
CloseConsole()
EndIf
EndIf</syntaxhighlight>
</syntaxhighlight>

{{out}}
{{out}}
<pre>
<pre>
Line 1,100: Line 1,093:
{{works with|QB64}}
{{works with|QB64}}
{{works with|VB-DOS}}
{{works with|VB-DOS}}
<syntaxhighlight lang="qbasic">
<syntaxhighlight lang="qbasic">DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER)
DECLARE FUNCTION SumDigits% (Num AS INTEGER, NBase AS INTEGER)


CLS
CLS
Line 1,120: Line 1,112:
LOOP
LOOP
SumDigits% = iSum
SumDigits% = iSum
END FUNCTION
END FUNCTION</syntaxhighlight>
</syntaxhighlight>

{{out}}
{{out}}
<pre>
<pre>
Line 1,149: Line 1,139:
print "fe base 16 : "; SumDigits(hexdec("FE"), 16)
print "fe base 16 : "; SumDigits(hexdec("FE"), 16)
print "f0e base 16 : "; SumDigits(hexdec("F0E"), 16)
print "f0e base 16 : "; SumDigits(hexdec("F0E"), 16)
end</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>


==={{header|TI-83 BASIC}}===
==={{header|TI-83 BASIC}}===
Line 1,216: Line 1,203:
PRINT "f0e base 16 :"; SumDigits(3854, 16) !0xf0e
PRINT "f0e base 16 :"; SumDigits(3854, 16) !0xf0e
END</syntaxhighlight>
END</syntaxhighlight>



==={{header|Visual Basic}}===
==={{header|Visual Basic}}===

This version checks that only valid digits for the indicated base are passed in, exiting otherwise.
This version checks that only valid digits for the indicated base are passed in, exiting otherwise.


Line 1,248: Line 1,233:
Debug.Print sumDigits("2", 2)
Debug.Print sumDigits("2", 2)
End Sub</syntaxhighlight>
End Sub</syntaxhighlight>

{{out}} (in the debug window):
{{out}} (in the debug window):
<pre>
<pre>
Line 1,257: Line 1,241:
0
0
</pre>
</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 (number, nBase)

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}}===
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub SumDigits(number, nBase)
<syntaxhighlight lang="vb">sub SumDigits(number, nBase)
if number < 0 then number = -number : fi
if number < 0 then number = -number : fi
if nBase < 2 then nBase = 2 : fi
if nBase < 2 then nBase = 2 : fi