Monday , September 25 2017
Home / java programs / Decimal To Hex Conversion Java Program

Decimal To Hex Conversion Java Program

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

About chandrashekhar

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+ ,

Recommended

Java program to find sum of digits

In this Java programming tutorials, I am going to show you how to calculate sum …

Leave a Reply

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