Java Tutorial: Print n in any Base: Another Example
Now, consider the problem of printing a decimal number in a base specified by the user. Recall that the solution for this is to use repetitive division and to write the remainders in reverse. The process terminates when the dividend is less than the base. Assume that the input decimal number is 10 and we want to convert it to base 8. Here is the solution using pen and paper.
Java Tutorial: Print n in any Base: Another Example
From the solution above, 10 is equivalent to 12 base 8.
Here is another example. The input decimal is 165 to be converted to base 16.
165 is equivalent to A5 base 16. Note: A=10.
This is the iterative solution for this problem.
Java Tutorial: Print n in any Base: Another Example
class DecToOthers {
public static void main(String args[]) {
int num = Integer.parseInt(args[0]);
int base = Integer.parseInt(args[1]);
printBase(num, base);
}
static void printBase(int num, int base) {
int rem = 1;
String digits = "0123456789abcdef";
String result = "";
/* the iterative step */
while (num!=0) {
rem = num%base;
num = num/base;
result = result.concat(digits.charAt(rem)+"");
}
/* printing the reverse of the result */
for(int i = result.length()-1; i >= 0; i--) {
System.out.print(result.charAt(i));
}
}
}
Java Tutorial: Print n in any Base: Another Example
Now, this is the recursive equivalent of the solution above.
class DecToOthersRecur {
static void printBase(int num, int base) {
String digits = "0123456789abcdef";
/* Recursive step*/
if (num >= base) {
printBase(num/base, base);
}
/* Base case: num < base */
System.out.print(digits.charAt(num%base));
}
public static void main(String args[]) {
int num = Integer.parseInt(args[0]);
int base = Integer.parseInt(args[1]);
printBase(num, base);
}
}
Java Tutorial: Print n in any Base: Another Example
To gain a better understanding of the code, trace it for a set of input. Here are some sample input.
a) Num: 10, base: 2
1010
b) Num: 100, base: 8
144
c) Num: 200, base: 9
242
Java Tutorial: Print n in any Base: Another Example
Now, consider the problem of printing a decimal number in a base specified by the user. Recall that the solution for this is to use repetitive division and to write the remainders in reverse. The process terminates when the dividend is less than the base. Assume that the input decimal number is 10 and we want to convert it to base 8. Here is the solution using pen and paper.
Java Tutorial: Print n in any Base: Another Example
From the solution above, 10 is equivalent to 12 base 8.
Here is another example. The input decimal is 165 to be converted to base 16.
165 is equivalent to A5 base 16. Note: A=10.
This is the iterative solution for this problem.
Java Tutorial: Print n in any Base: Another Example
class DecToOthers {
public static void main(String args[]) {
int num = Integer.parseInt(args[0]);
int base = Integer.parseInt(args[1]);
printBase(num, base);
}
static void printBase(int num, int base) {
int rem = 1;
String digits = "0123456789abcdef";
String result = "";
/* the iterative step */
while (num!=0) {
rem = num%base;
num = num/base;
result = result.concat(digits.charAt(rem)+"");
}
/* printing the reverse of the result */
for(int i = result.length()-1; i >= 0; i--) {
System.out.print(result.charAt(i));
}
}
}
Java Tutorial: Print n in any Base: Another Example
Now, this is the recursive equivalent of the solution above.
class DecToOthersRecur {
static void printBase(int num, int base) {
String digits = "0123456789abcdef";
/* Recursive step*/
if (num >= base) {
printBase(num/base, base);
}
/* Base case: num < base */
System.out.print(digits.charAt(num%base));
}
public static void main(String args[]) {
int num = Integer.parseInt(args[0]);
int base = Integer.parseInt(args[1]);
printBase(num, base);
}
}
Java Tutorial: Print n in any Base: Another Example
To gain a better understanding of the code, trace it for a set of input. Here are some sample input.
a) Num: 10, base: 2
1010
b) Num: 100, base: 8
144
c) Num: 200, base: 9
242
Java Tutorial: Print n in any Base: Another Example



