Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class Program {
- public static void decToBinString(int number) {
- String bin = "";
- while (number != 0) {
- bin += number % 2;
- number /= 2;
- }
- // reverse
- for(int i = bin.length() - 1; i >= 0; i--) {
- System.out.print(bin.charAt(i));
- }
- }
- public static void decToBinStack(int number) {
- Stack<Integer> bin = new Stack<Integer>();
- while (number != 0) {
- bin.push(number % 2);
- number /= 2;
- }
- while (!bin.empty()) {
- System.out.print(bin.pop());
- }
- }
- public static int binToDec(int bin) {
- int dec = 0;
- int power = 0;
- while (bin != 0) {
- dec += (bin % 10) * Math.pow(2, power);
- power++;
- bin /= 10;
- }
- return dec;
- }
- public static void main(String[] args) {
- while (true) {
- System.out.println("1. BIN -> DEC");
- System.out.println("2. DEC -> BIN");
- System.out.println("3. EXIT");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement