Java Program to Convert a Decimal Number to Binary Number

Java Program to Convert a Decimal Number to Binary Number


Java Program to Convert a Decimal Number to Binary Number

Following Java program convert Decimal number to Binary number. Enter any integer as an input. Now we convert the given decimal input into a binary number with the help of String. We have taken a number in decimal and then done modulus division by 2, the remainder is added/concatenated to a binary number which is a String variable. The program output is also shown below.



Image from wikimedia.org


Decimal Number to Binary Number 

In the program below, a number in Decimal format has been taken from the keyboard and passed to toBinary(int num) method. Where the number is converted by mode division by 2 and added to variable binaryNumber till the number becomes zero. And then return the value. 
Source Code:
import java.io.*;
public class DecimalToBinary {

    String binaryNumber;        // Calculate the number into Binary as String
    int rem;                    // Store the remainder as int
    DecimalToBinary()
    {
        binaryNumber="";
        rem=0;
    }
    // Binary Method
    public String toBinary(int num)
    {
        if (num == 0)
        {
            return "0";
        }

        while (num > 0)
        {
            rem = num % 2;
            binaryNumber = rem + binaryNumber;
            num = num / 2;
        }
        return binaryNumber;
    }
    // Start main method
    public static void main(String[] args) throws IOException
    {
        int decimal;
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        DecimalToBinary ob = new DecimalToBinary();
        System.out.print("Enter a number to convert: ");
        decimal=Integer.parseInt(br.readLine());
        String bin = ob.toBinary(decimal); // Decimal to Binary method calling
        System.out.println("The binary number representation is " + bin);

    } // End of main method
}

Output:
Enter a number to convert: 100
The binary number representation is 1100100
Enter a number to convert: 15
The binary number representation is 1111
Enter a number to convert: 25
The binary number representation is 11001


We can convert decimal to binary in java using Integer.toBinaryString() method also

Source Code:
public class DecimalToBinaryWithMethod
    public static void main(String args[])
    { 
        Integer n1 = new Integer(100);
        Integer n2 = new Integer(15);
        Integer n3 = new Integer(25);
        // Convertion using toBinaryString() method
        String binary1 = Integer.toBinaryString(n1); // Method toBinaryString(n1)
        String binary2 = Integer.toBinaryString(n2);
        String binary3 = Integer.toBinaryString(n3);
        System.out.println(binary1);
        System.out.println(binary2);
        System.out.println(binary3);
    }


Output:
1100100
1111
11001
 In the above program we have used  Integer.toBinaryString() method to convert directly decimal number to binary number and store as String.


More Program



SHARE THIS
Previous Post
Next Post