Java program to convert decimal number to binary number
The program for conversion of decimal to binary depends on the problem specification. It may be given that the decimal number is always positive or it can be both positive and negative. Another thing to consider is the number of bits we are allotted for the binary representation. Finite number of bits in binary cannot represent any arbitrary large decimal number. The program given here assumes that the decimal number that we are given as input is positive. We first explain the procedure for converting a positive decimal number to a binary number and then give the program.
Procedure
First, let us see how humans perform the computation. We start with the given decimal number, and repeatedly divide it by 2 (whole number division where the result will be only the quotient, a whole number) and note down the remainder at every step till we obtain the quotient as 0. The remainders obtained in each step when concatenated together (the last remainder taken as the first digit of the binary number) give the binary form of the decimal number.
Shown below is the repeated division of 14.
The remainders obtained when concatenated together give the binary representation of 14 which is 1110.
Java Program
The program is given below. The explanation follows it.
import java.util.Scanner;
public class DecimalToBinary {
public String toBinary(int n) {
if (n == 0) {
return "0";
}
String binary = "";
while (n > 0) {
int rem = n % 2;
binary = rem + binary;
n = n / 2;
}
return binary;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int decimal = scanner.nextInt();
DecimalToBinary decimalToBinary = new DecimalToBinary();
String binary = decimalToBinary.toBinary(decimal);
System.out.println("The binary representation is " + binary);
}
}
The method toBinary() accepts an integer n that should be converted to binary format. As we said earlier, the number n should be repeatedly divided by 2 till we obtain 0 and the remainders should be concatenated. We use the String variable 'binary' to hold the result. The while loop has the condition n!=0 which will repeatedly perform the operations in the while block as long as n is not zero.
The while loop contains three statements.
The first is finding the remainder on dividing n by 2 which is obtained by using the modulo operator.
The next step concatenates the remainder to the binary representation we have obtained so far. Note that we have written 'rem + binary' and not 'binary + rem'. This is because the last remainder will be the first bit in the binary representation. Consider that just before the last division, we have binary = 100. If the last step gives rem = 1, then 'rem + binary' will be 1100 which is correct as the last remainder was placed in the beginning of the binary representation. If we had used 'binary + rem', the result would have been '1001' which is incorrect.
The third statement in the while loop updates the value of n to n/2 i.e. it divides the value by 2 and takes the quotient. Note that both n and 2 are integers which on division will return an integer and not a real number.
Now, let us see what will happen if we invoke the method toBoolean() with decimal = 0 (when the special check for n == 0 is not present). The variable binary will be initialised to "". The condition n!=0 is false, so the loop will not be executed even once and the result returned would be "". But the correct answer is "0". So, we add a special check for decimal = 0 and return "0" as the result.
Given below is a sample output.
Enter a number: 14
The binary representation is 1110
