Sum digits of an integer: Difference between revisions

→‎{{header|RPL}}: slightly improved
(→‎{{header|RPL}}: slightly improved)
Line 4,110:
RPL can natively handle numbers in bases 2,8,10 or 16, but displays them according to the current base mode. For example, when you type #256d, it will be immediately turned into #100h if HEX mode is active. As there is no way to force the base mode to the base used for input, switching to string handling looks like a reasonable approach for code clarity and size. A side effect is that it can proceed with numbers in any base between 2 and 16.
{{works with|Halcyon Calc|4.2.7}}
≪ →STR 0"0123456789ABCDEF" → n digits
1 30 1 PICKn SIZE '''FOR''' j
"0123456789ABCDEF" 3 PICK digits n j DUP SUB POS
POS DUP 1 - 0 IFTE +
'''NEXT'''
≫ ≫ '<span style="color:blue">DIGITS</span>' STO
SWAP DROP
'DIGITS' STO
 
1 <span style="color:blue">DIGITS</span>
1234 <span style="color:blue">DIGITS</span>
#FEh <span style="color:blue">DIGITS</span>
#F0Eh <span style="color:blue">DIGITS</span>
{{out}}
<pre>
1,150

edits