The problem
Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. Here are some sample runs:
Enter a decimal value (0 to 15): 11
The hex value is B
Breaking it down
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a decimal value (0 to 15): ");
int number = input.nextInt();
input.close();
if (number < 0 || number > 15) {
System.out.println("Invalid input");
} else {
String hexString = Integer.toHexString(number).toUpperCase();
System.out.println(hexString);
}
}
Output
Enter a decimal value (0 to 15): 12
C