Advertisement
vencinachev

BINDEC

Jan 18th, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6. import java.util.Stack;
  7.  
  8. public class Program {
  9.    
  10.     static void DecToBin(int number) {
  11.         Stack<Integer> digits = new Stack<Integer>();
  12.         while (number != 0) {
  13.             digits.push(number % 2);
  14.             number /= 2;
  15.         }
  16.        
  17.         while (digits.size() != 0) {
  18.             System.out.print(digits.pop());
  19.         }
  20.     }
  21.    
  22.     static int BinToDec (int bin) {
  23.         int dec = 0;
  24.         int power = 0;
  25.         while (bin != 0){
  26.             int an = bin % 10;
  27.             dec += an * Math.pow(2, power);
  28.             power++;
  29.             bin /= 10;
  30.         }
  31.         return dec;
  32.     }
  33.    
  34.     public static void main(String[] args) {
  35.         //DecToBin(200);
  36.         System.out.println(BinToDec(11001000));
  37.     }
  38.  
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement