Categories

# Convert Decimal to Octal and Hexadecimal using Recursion

Write a program to allow the user to enter a positive integer.

Now convert that number into octal (base 8) and hexadecimal (base 16) and display the results with appropriate message.

Note that the conversion needs to be done using recursive methods.

Example:
INPUT:
N = 999
Octal equivalent: 1747

``````import java.io.*;
class Convert{
public static void main(String args[])throws IOException{
System.out.print("Enter the number in decimal: ");
int oct = Integer.parseInt(octal(n));
System.out.println("Octal equivalent: " + oct);
}
public static String octal(int num){
if(num < 8)
return Integer.toString(num);
else
return octal(num / 8) + "" + num % 8;
}
if(num < 10)
return Integer.toString(num);
else if(num < 16){
switch(num){
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "";
}
}
else
}
}``````

Design a class Change to convert a decimal number to its equivalent in base 16 and to convert it back to its decimal form.

Examples:
The decimal number 35 is 23 in base 16.
The decimal number 107 is 6B in base 16.

Some of the members of the class are given below:
Class name: Change
Data members:
n : integer to be converted to base 16.
Member functions:
Change(): constructor to assign 0 to the instance variables.
void input(): accepts integer to be converted to base 16.
void deciHexa(String): to convert the hexadecimal number back to decimal form.

Specify the class change giving the details of the constructor and the functions void input(), void hexaDeci(int) and void deciHexa(String).

The main function need not be written.

``````import java.io.*;
class Change{
int n;
public Change(){
n = 0;
}
public void input()throws IOException{
System.out.print("Enter integer in base 10: ");
}
String h = "";
if(num == 0)
h = "0";
while(num != 0){
int rem = num % 16;
if(rem < 10)
h = rem + h;
else{
switch(rem){
case 10:
h = "A" + h;
break;
case 11:
h = "B" + h;
break;
case 12:
h = "C" + h;
break;
case 13:
h = "D" + h;
break;
case 14:
h = "E" + h;
break;
case 15:
h = "F" + h;
break;
}
}
num /= 16;
}
deciHexa(h);
}
public void deciHexa(String h){
int d = 0;
int p = 0;
for(int i = h.length() - 1; i >= 0; i--){
char ch = h.charAt(i);
switch(ch){
case 'A':
d += 10 * (int)Math.pow(16, p++);
break;
case 'B':
d += 11 * (int)Math.pow(16, p++);
break;
case 'C':
d += 12 * (int)Math.pow(16, p++);
break;
case 'D':
d += 13 * (int)Math.pow(16, p++);
break;
case 'E':
d += 14 * (int)Math.pow(16, p++);
break;
case 'F':
d += 15 * (int)Math.pow(16, p++);
break;
default:
int digit = Integer.parseInt(Character.toString(ch));
d += digit * (int)Math.pow(16, p++);
}
}
System.out.println("Back to decimal: " + d);
}
public static void main(String args[])throws IOException{
Change obj = new Change();
obj.input();
}
}``````

## 4 replies on “Convert Decimal to Octal and Hexadecimal using Recursion”

sayan ranasays:

Sir,how to convert this recursive function to iterative code?
public static String octal(int num){
if(num < 8)
return Integer.toString(num);
else
return octal(num / 8) + "" + num % 8;
}

String octal = “”;
while(num > 0){
octal = (num % 8) + octal;
num /= 8;
}

sayan ranasays:

Sir,how to convert this recursive function to iterative code??
if(num < 10)
return Integer.toString(num);
else if(num < 16){
switch(num){
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "";
}
}
else