Decimal To Hex Conversion Java Program

By | 2018-03-21T21:03:51+00:00 June 18th, 2016|java programs|0 Comments

Here we are going to write a program on conversion of Decimal To Hex Numbers in Java.

Decimal To Hex Conversion :


import java.util.Scanner;

public class ConvertDecimalToHexaDecimal {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any Decimal Number : ");
        int input = sc.nextInt();
        System.out.println(" Hex : " + convertDecimalToHexaDecimal(input));

    }

    public static String convertDecimalToHexaDecimal(int decimal) {
        String hexaDecimal = "";
        String hex = "0123456789ABCDEF";
        try {
            if (decimal != 0) {
                while (decimal > 0) {
                    hexaDecimal = hex.charAt(decimal % 16) + hexaDecimal;
                    decimal /= 16;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            hex = null;
        }
        return hexaDecimal;
    }
}

Output :

Enter any Decimal Number : 505
Hex : 1F9

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment