Advertisement
Krassi_Daskalova

Decimal to Binary

Jan 26th, 2022
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class DecimalToBinaryConverter {
  5.     public static void main(String[] args){
  6.         Scanner scanner = new Scanner(System.in);
  7.         int decimal = Integer.parseInt(scanner.nextLine());
  8.         ArrayDeque <Integer> stack = new ArrayDeque<>();
  9.         if(decimal == 0){
  10.             System.out.println("0");
  11.         }
  12.  
  13.         while (decimal != 0){
  14.             stack.push(decimal % 2);
  15.             decimal /= 2;
  16.  
  17.         }
  18.         while (!stack.isEmpty()){
  19.             System.out.print(stack.pop());
  20.         }
  21.  
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement