forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
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 mouredev#2942 from LaijieJi/patch-5
Reto mouredev#14 - Java
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/java/LaijieJi.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,41 @@ | ||
import java.util.*; | ||
|
||
public class LaijieJi { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
String octal = ""; | ||
String hex = ""; | ||
System.out.println("Please, write the decimal number"); | ||
int decimal = sc.nextInt(); | ||
octal = decToOctal(decimal); | ||
hex = decimalToHexadecimal(decimal); | ||
System.out.println(decimal + " in Octal is: " + octal + ", and in Hex it is : " + hex); | ||
sc.close(); | ||
} | ||
|
||
public static String decToOctal(int decimal){ | ||
int over; | ||
String res = ""; | ||
char[] octals = {'0', '1', '2', '3', '4', '5', '6', '7'}; | ||
while (decimal > 0){ | ||
over = decimal % 8; | ||
char character = octals[over]; | ||
res = character + res; | ||
decimal = decimal / 8; | ||
} | ||
return res; | ||
} | ||
|
||
public static String decimalToHexadecimal(int decimal) { | ||
int over; | ||
String hexadecimal = ""; | ||
char[] HexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | ||
while (decimal > 0) { | ||
over = decimal % 16; | ||
char caracterHexadecimal = HexChars[over]; | ||
hexadecimal = caracterHexadecimal + hexadecimal; | ||
decimal = decimal / 16; | ||
} | ||
return hexadecimal; | ||
} | ||
} |