Advertisement
meteor4o

JA-Stacks-Lab-03.DecimalToBinary

Aug 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6.  
  7. class Main {
  8.   public static void main(String[] args) {
  9.  
  10.   Scanner sc = new Scanner(System.in);
  11.  
  12.   ArrayDeque<Integer> binaryStack = new ArrayDeque<>();
  13.  
  14.   int decimal = sc.nextInt();
  15.   String binary = "";
  16.  
  17.     if (decimal == 0) {
  18.       binary = "0";
  19.     } else {
  20.  
  21.     while (decimal != 0) {
  22.       binaryStack.push(decimal % 2);
  23.       decimal /= 2;
  24.     }
  25.    
  26.  
  27.     while(!binaryStack.isEmpty()) {
  28.       binary += binaryStack.pop();
  29.     }
  30.   }
  31.  
  32.   System.out.print(binary);
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement