Program on Decimal to Hexadecimal Number Conversion


Write a Program in Java to input a number in Decimal number system and convert it into its equivalent number in the Hexadecimal number system.

Note: Hexadecimal Number system is a number system which can represent a number in any other number system in terms of digits ranging from 0 to 9 and then A – F only. This number system consists of only sixteen basic digits i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Here 10 is represented as A, 11 as B and so on till 15 which is represented as F.

For Example: 47 in the Decimal number system can be represented as 2F in the Hexadecimal number system.

hexadecimal to binary

Figure Illustrating Decimal to Hexadecimal Number System Conversion


* The class Dec2Hex inputs a Decimal number and converts it into its equivalent Hexadecimal number
* @author :
* @Program Type : BlueJ Program - Java

class Dec2Hex
    public static void main(String args[])throws IOException
        BufferedReader br=new BufferedReader (new InputStreamReader(;
        System.out.print("Enter a decimal number : ");
        int n=Integer.parseInt(br.readLine());

        int r;
        String s=""; //variable for storing the result

        //array storing the digits (as characters) in a hexadecimal number system
        char dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

                r=n%16; //finding remainder by dividing the number by 16
                s=dig[r]+s; //adding the remainder to the result
        System.out.println("Output = "+s);


Enter a decimal number : 47
Output = 2F

Enter a decimal number : 1243
Output = 4DB

[Note: Don’t forget to share these resources and links from our website in your social networking sites with your friends and followers.]

A teacher whose aim is to make Java a fun to learn experience for the school students by striving to build clear fundamentals of every concepts and helping students to develop their ability to reason and logically analyze any given programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This