Skip to content

Commit

Permalink
Finished the challenge.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrián Hernández committed Apr 3, 2023
1 parent 9e0a56e commit 411bcc8
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/java/thiestoril.java
Original file line number Diff line number Diff line change
@@ -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 <Integer, String> 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 <Integer, String> dict = new HashMap <Integer, String>();
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);
}
}

0 comments on commit 411bcc8

Please sign in to comment.