Convert decimal number to base 2-9: Difference between revisions

Blanked the page
No edit summary
(Blanked the page)
 
Line 1:
{{Draft task}}
 
;Task:Write a function to convert decimal positive integer number '''n''' to base '''2-9'''
<br>
Do it even your language has built-in function for it.
 
<br><br>
 
=={{header|Ring}}==
<lang ring>
see "working..." + nl
dec = 192
 
for n = 2 to 9
db = decimaltobase(dec,n)
see "decimal number " + dec + " in base " + " + n + is: " + db + nl
next
 
see "done..." + nl
 
func decimaltobase(nr,base)
decList = 0:15
baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
 
binList = []
binary = 0
remainder = 1
while(nr != 0)
remainder = nr % base
ind = find(decList,remainder)
rem = baseList[ind]
add(binList,rem)
nr = floor(nr/base)
end
binlist = reverse(binList)
binList = list2str(binList)
binList = substr(binList,nl,"")
return binList
</lang>
{{out}}
<pre>
working...
decimal number 192 in base 2 is: 11000000
decimal number 192 in base 3 is: 21010
decimal number 192 in base 4 is: 3000
decimal number 192 in base 5 is: 1232
decimal number 192 in base 6 is: 520
decimal number 192 in base 7 is: 363
decimal number 192 in base 8 is: 300
decimal number 192 in base 9 is: 233
done...
 
</pre>
2,468

edits