Java Program to Convert Hexadecimal to Octal

« Previous Program Next Program »

Convert Hexadecimal to Octal

To convert hexadecimal to octal in Java Programming, you have to ask to the user to enter any number in hexadecimal number format to convert it into octal number format to display the equivalent value in octal number system as shown in the following program.

Java Programming Code to Convert Hexadecimal to Octal

Following Java Program ask to the user to enter hexadecimal number to convert it into octal number, then display the result on the screen:

Here first we convert the hexadecimal number into decimal number then we convert it into octal number.

/* Java Program Example - Convert Hexadecimal to Octal */
import java.util.Scanner;

public class JavaProgram
    public static int hex2decimal(String s)
             String digits = "0123456789ABCDEF";
             s = s.toUpperCase();
             int val = 0;
             for (int i = 0; i < s.length(); i++)
                 char c = s.charAt(i);
                 int d = digits.indexOf(c);
                 val = 16*val + d;
             return val;
    public static void main(String args[])
        String hexdecnum;
        int decnum, i=1, j;
        int octnum[] = new int[100];
        Scanner scan = new Scanner(;
        System.out.print("Enter Hexadecimal Number : ");
        hexdecnum = scan.nextLine();
        // first convert hexadecimal to decimal
        decnum = hex2decimal(hexdecnum);
        // now convert decimal to octal
        while(decnum != 0)
            octnum[i++] = decnum%8;
            decnum = decnum/8;
        System.out.print("Equivalent Octal Number is :\n");
        for(j=i-1; j>0; j--)

When the above Java Program is compile and executed, it will produce the following output:

Java Program convert hexadecimal to octal

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:

Java Online Test

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.