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
Hexadecimal equivalent: 3E7

import java.io.*;
class Convert{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number in decimal: ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        int oct = Integer.parseInt(octal(n));
        String hex = hexadecimal(n);
        System.out.println("Octal equivalent: " + oct);
        System.out.println("Hexadecimal equivalent: " + hex);
    }
    public static String octal(int num){
        if(num < 8)
            return Integer.toString(num);
        else
            return octal(num / 8) + "" + num % 8;
    }
    public static String hexadecimal(int num){
        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
            return hexadecimal(num / 16) + "" + hexadecimal(num % 16);
    }
}

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 hexaDeci(int): to convert decimal integer to hexadecimal form.
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{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter integer in base 10: ");
        n = Integer.parseInt(br.readLine());
    }
    public void hexaDeci(int num){
        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;
        }
        System.out.println("Hexadecimal equivalent: " + h); 
        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();
        obj.hexaDeci(obj.n);
    }
}

4 thoughts on “Convert Decimal to Octal and Hexadecimal using Recursion

  1. 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;
    }

  2. Sir,how to convert this recursive function to iterative code??
    public static String hexadecimal(int num){
    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
    return hexadecimal(num / 16) + "" + hexadecimal(num % 16);
    }
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.