Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class Principal {
- public static void main(String[] args) {
- Scanner S = new Scanner(System.in);
- char resp;
- do {
- System.out.print("Ingrese un numero entero(BASE 10): ");
- int D = S.nextInt();
- Convertir(D);
- System.out.println();
- System.out.println("Desea convertir otro numero? s/n");
- resp = S.next().charAt(0);
- System.out.println();
- }while(resp == 's' || resp == 'S');
- System.out.println("Fin del Programa...");
- }
- public static void Convertir(int D) {
- Stack<Integer> p1 = new Stack<Integer>();
- int C,R;
- do {
- C = D / 2;
- R = D % 2;
- p1.push(R);
- D = C;
- }while(D >= 2);
- p1.push(D);
- Acomodar(p1);
- }
- public static void Acomodar(Stack<Integer>p1) {
- Integer[]datos=new Integer[p1.size()];
- p1.toArray(datos);
- System.out.print("El numero en base binaria es: ");
- for(int i=datos.length-1; i>=0; i--) {
- System.out.print(datos[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement