Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p

decimal to binary Java program

Decimal to binary

It's easy to convert a number from decimal to binary and even it's reverse conversion from binary to decimal.

Algorithm to convert a number from binary to decimal

To convert from base 10 to 2, we just divide the decimal number by 2 until we get 1 and write the all the remainders after it in reverse order(from last found remainder to the first ). Look at the flowchart below to better understand it.

how to convert decimal to binary

Start with n and check if n is equal to 1. If the n isn't equal to 1 divide it by 2 and update the n with the quotient and push the remainder to stack, do it until the n become 1.

If the n is 1 pop out all the element from the stack (if exists) and write them after the 1 in order, this way n number will be converted into binary.

Decimal to binary - java program

There are different ways to get it done in java one easy way you can use java's Integer.toBinaryString() that takes one decimal number or Integer.toString() that takes two parameters first one the decimal number which has to be convert and the second one is base in which number has to be convert.

These methods already exist to convert. However, it is not what you're looking for right! instead, you just wanna try to implement your own logic.

The second way you have seen above. I suggest you don't see the program, first, try it yourself .

And the third is using the recursion technique.

First: Java toBinaryString() method to get the binary representation

    class Main {
public static void main(String[] args) {
// decimal digit
int n = 8;
System.out.println("Binary: " + convertToBinary(n));
}

private static String convertToBinary(int n) {
return Integer.toBinaryString(n); // or Integer.toString(n, 2)
}

}

Run it in Repl.it (An online Ide)

Program Output

Binary: 1000

Second: Java Program to convert decimal to binary with a stack

    import java.util.Stack;

class Main {
public static void main(String[] args) {
// decimal digit
int n = 8;
System.out.println("Binary: " + convertToBinary(n));
}

private static String convertToBinary(int n) {
Stack<Integer> stack = new Stack();
    // while the n is not 1
while (n != 1) {
stack.push(n % 2);
n = n / 2;
}
String bin = "1";    // while the stack is not empty
while (!stack.empty()) {
bin = bin + stack.pop();
}

return bin;
}
}

Repl.it

Program Output

Binary: 1000

Third: decimal to binary using recursion in java

import java.util.Stack;

class Main {
public static void main(String[] args) {
// decimal digit
int n = 15;
System.out.println("Binary: " + convertToBinary(n));
}

private static String convertToBinary(int n) {
if (n == 1) return "1";
return convertToBinary(n / 2) + n % 2;
}
}

Repl.it

Program Output

Binary: 1111

Comment below if you have any doubts or Know any other different way to convert decimal to binary.

You May also like my other posts 

No comments:

Post a Comment