Skip to content

Commit

Permalink
Merge pull request mouredev#2942 from LaijieJi/patch-5
Browse files Browse the repository at this point in the history
Reto mouredev#14 - Java
  • Loading branch information
Roswell468 authored Apr 8, 2023
2 parents eba8c7b + dd46e10 commit 74a32d5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/java/LaijieJi.java
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;
}
}

0 comments on commit 74a32d5

Please sign in to comment.