Using Recursion to Convert Number to Other Number Bases
Using Recursion to Convert Number to Other Number Bases
Data Structures in Java with JUnit ?Rick Mercer
9910 is also 11000112
Problem: Convert a decimal (base 10) number into other bases
Message
Return
convert(99, 2) convert(99, 3) convert(99, 4) convert(99, 5) convert(99, 6) convert(99, 7) convert(99, 8) convert(99, 9)
"1100011" "10200" "1203" "344" "243" "201" "143" "120"
Multiply Digits by Powers of Base
10, 8, 2, or whatever
Decimal numbers: multiply digits by powers of 10
950710 = 9 x 103 + 5 x 102 + 0 x 101 + 7 x 100
Octal numbers powers of 8
15678 = 1 x 83 + 5 x 82 + 6 x 81 + 7 x 80
= 512 + 320 + 48 + 7
= 88710
Binary numbers powers of 2
11012 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
=8 +4
+ 0
+ 1
= 1310
Converting base 10 to base 2
1) divide number (5) by new base(2), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left
__2_ 2 ) 5
__1_ 2 ) 2
__0_ 2 ) 1
Remainder = 1 Remainder = 0 Remainder = 1
Stop when the quotient is 0
510 = 1012
Place remainders in reverse order
Converting base 10 to base 8
1) divide number by new base (8), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left
_12_ 8 )99 Remainder = 3
__1_ 8 )12 Remainder = 4
__0_ 8 ) 1 Remainder = 1
Stop when the quotient is 0
9910 = 1438
Place remainders in reverse order
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.