Advertisement
vencinachev

BinToDecJava

Feb 18th, 2021
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class Program {
  4.  
  5.     public static void decToBinString(int number) {
  6.         String bin = "";
  7.         while (number != 0) {
  8.             bin += number % 2;
  9.             number /= 2;
  10.         }
  11.         // reverse
  12.         for(int i = bin.length() - 1; i >= 0; i--) {
  13.             System.out.print(bin.charAt(i));
  14.         }
  15.     }
  16.    
  17.     public static void decToBinStack(int number) {
  18.         Stack<Integer> bin = new Stack<Integer>();
  19.         while (number != 0) {
  20.             bin.push(number % 2);
  21.             number /= 2;
  22.         }
  23.        
  24.         while (!bin.empty()) {
  25.             System.out.print(bin.pop());
  26.         }
  27.     }
  28.    
  29.     public static void main(String[] args) {
  30.         decToBinString(25);
  31.         System.out.println();
  32.         decToBinStack(25);
  33.     }
  34.  
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement