import javax.swing.JOptionPane;
public class DecimalAOctal {
public static void main(String[] args) {
String strDecimal = JOptionPane.showInputDialog(null, "Introduce un número decimal: ",
"Decimal a Octal", JOptionPane.QUESTION_MESSAGE);
int decimal = Integer.parseInt(strDecimal);
System.out.println("Decimal a Octal");
String octal = Integer.toOctalString(decimal);
System.out.printf("%nMétodo 1 -> Decimal: %d, Octal: %s", decimal, octal);
char digitosO[]={'0','1','2','3','4','5','6','7'};
String octal2 = "";
int resto, aux = decimal;
while(aux>0){
resto = aux % 8;
octal2 = digitosO[resto] + octal2;
aux /= 8;
}
System.out.printf("%n%nMétodo 2 -> Decimal: %d, Octal: %s", decimal, octal2);
}
}
0 Comentarios