From 411bcc89e6ad3091c50e988b9ae3779dd7a7e9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Hern=C3=A1ndez?= Date: Mon, 3 Apr 2023 23:23:58 +0200 Subject: [PATCH] Finished the challenge. --- .../java/thiestoril.java" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/java/thiestoril.java" diff --git "a/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/java/thiestoril.java" "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/java/thiestoril.java" new file mode 100644 index 0000000000..bd395f4084 --- /dev/null +++ "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/java/thiestoril.java" @@ -0,0 +1,62 @@ +import java.util.Scanner; +import java.util.HashMap; + + +public class thiestoril { + + // Converts an integer to an octal number + private static String intToOct(int number) { + String octal = ""; + + while (number != 0) { + octal = (number % 8) + octal; + number /= 8; + } + + return octal; + } + + // Converts an integer to a hexadecimal + private static String intToHex(int number, HashMap dic) { + String hex = ""; + int aux; + + while (number != 0) { + aux = number % 16; + hex = (aux < 10) ? aux + hex : dic.get(aux) + hex; + number /= 16; + } + + return hex; + } + + public static void main(String args[]) { + + // Get the input + System.out.println("Insert a number:"); + + Scanner scan = new Scanner(System.in); + String input = scan.next(); + + int number = Integer.parseInt(input); + + // Setup dictionary + HashMap dict = new HashMap (); + dict.put(10, "A"); + dict.put(11, "B"); + dict.put(12, "C"); + dict.put(13, "D"); + dict.put(14, "E"); + dict.put(15, "F"); + + // Convert to octal + String octal = intToOct(number); + + // Convert to hex + String hex = intToHex(number, dict); + + // Print the results + System.out.println("Octal is: " + octal); + System.out.println("Hexadecimal is: " + hex); + } +} \ No newline at end of file