Output
Variable Name   Value in Memory
empty string
0
1
2
3
4
5
6
7
8
9
a
a
a
a
a
a
a
a
a
a
1 import java.util.Scanner;
2
3 public class Dec2Hex {
4
5 public static void main(String[] args) {
6
7 Scanner input = new Scanner(System.in);
8
9
10 System.out.print("Enter a decimal number: ");
11 int decimal = input.nextInt();
12
13
14 String hex = "";
15
16 while (decimal != 0) {
17 int hexValue = decimal % 16;
18
19
20 char hexDigit = (0 <= hexValue && hexValue <= 9) ?
21 (char)(hexValue + '0') : (char)(hexValue - 10 + 'A');
22
23 hex = hexDigit + hex;
24 decimal = decimal / 16;
25 }
26
27 System.out.println("The hex number is " + hex);
28 }
29 }