HEX. (Decimal to hex) Write a program that prompts the user to enter an integer
ID: 3563349 • Letter: H
Question
HEX. (Decimal to hex) Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. INPUT and PROMPTS. The program prompts for a integer with: "Enter a decimal value (0 to 15): ". OUTPUT . If the integer read in is equal to the value of a single hexadecimal digit (i.e. in the range specified by the prompt), the program prints out "The hex value is x", where x is the hexadecimal digit corresponding to the input; otherwise it prints the message "Invalid input". CLASS NAMES. Your program class should be called Hex
Can you please help me with this code I am getting a lot of errors.
Explanation / Answer
import java.util.Scanner;
public class Hex
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int dec;
int x;
System.out.print( "Enter a decimal value (0 to 15):" );
dec = input.nextInt();
if(dec>=0 && dec<=15)
{
while(dec!=0)
{
x=dec%16;
if(x<=9)
{
System.out.printf( "The hex value is %d ", x);
exit(0);
}
dec=dec/16;
}
switch(x)
{
case 10:System.out.printf("A");
break;
case 11:System.out.printf("B");
break;
case 12:System.out.printf("C");
break;
case 13:System.out.printf("D");
break;
case 14:System.out.printf("E");
break;
case 15:System.out.printf("F");
break;
}
}
else
{
System.out.print("Invalid input");
exit(0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.