Advertisement
ShSh99

DecimalToBinary

Nov 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class DecimalToBinary {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. int num = Integer.parseInt(sc.nextLine());
  9.  
  10. ArrayDeque<Integer> stack = new ArrayDeque<>();
  11. if(num == 0){
  12. stack.push(0);
  13. }
  14. while (num != 0){
  15. int remain = num % 2;
  16. stack.push(remain);
  17. num /= 2;
  18. }
  19. for (int i = stack.size(); i > 0; i--) {
  20. System.out.print(stack.pop());
  21. }
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement