-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2878 from ThiEstoRil/main
Reto #14 - Java
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/java/thiestoril.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |