Java if statement convert decimal to hex

Question

We would like to 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 

Enter a decimal value (0 to 15): 5 
The hex value is 5 

Enter a decimal value (0 to 15): 31 
31 is an invalid input 
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter an integer between 0 and 15
    System.out.print("Enter a decimal value (0 to 15): ");
    int decimal = input.nextInt();

    //your code here
  }/*from   w  w  w .j  a va 2s.c o m*/
}



import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter an integer between 0 and 15
    System.out.print("Enter a decimal value (0 to 15): ");
    int decimal = input.nextInt();

    // Display its corresponding hex number
    if (decimal >= 0 && decimal <= 9)
      System.out.println("The hex value is " + decimal);
    else if (decimal >= 10 && decimal <= 15)
      System.out.println("The hex value is " + (char)(decimal + 'A' - 10));
    else
      System.out.println(decimal + " is an invalid input");
  }
}



PreviousNext

Related