Java Program to Convert Decimal to Hexadecimal

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

Java Programming Code to Convert Decimal to Hexadecimal

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

```/* Java Program Example - Convert Decimal to Hexadecimal */

import java.util.Scanner;

public class JavaProgram
{
public static void main(String args[])
{
int decnum, rem;
String hexdecnum="";

/* digits in hexadecimal number system */

char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

Scanner scan = new Scanner(System.in);

System.out.print("Enter Decimal Number : ");
decnum = scan.nextInt();

while(decnum>0)
{
rem = decnum%16;
hexdecnum = hex[rem] + hexdecnum;
decnum = decnum/16;
}

System.out.print("Equivalent Hexadecimal Value of " + decnum + " is :\n");
System.out.print(hexdecnum);
}
}```

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

Same Program in Other Languages

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

Tools
Calculator