Kali ini saya akan membagikan cara konversi bilangan decimal ke hexa, sebelum kalian coba, ada baiknya kalian tau cara manualnya sebelum praktek pada pemrograman java, . . Silahkan dicoba, .
import java.util.Scanner;
public class DecToHex {
public static void main(String[] iriahus) {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.print("Enter a decimal value (0 to 15): ");
int dec = s.nextInt();
int hex;
if (dec >= 0 && dec < 10) {
hex = dec;
System.out.println("The hex value is " + hex);
}
else if (dec > 9 && dec < 16) {
hex = dec % 10;
switch (hex) {
case 0:
System.out.println("The hex value is A");
break;
case 1:
System.out.println("The hex value is B");
break;
case 2:
System.out.println("The hex value is C");
break;
case 3:
System.out.println("The hex value is D");
break;
case 4:
System.out.println("The hex value is E");
break;
case 5:
System.out.println("The hex value is F");
break;
default:
break;
}
} else {
System.out.println("Invalid input!");
}
}
}
Semoga bermanfaat, . .
0 Response to "Konversi Bilangan Decimal Ke Hexadecimal"
Post a Comment