Java Convert Decimal to Binary

In this source code example, we will write a Java program that converts any Decimal number to a Binary number.
We show you two ways to convert any Decimal number to a Binary number:
  1. The first method converts a decimal number to a binary number using a conventional algorithm.
  2. The second method converts a decimal number to a binary number using a bitwise algorithm
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 50+ Java Programs 

Convert Decimal number to Binary number using a Conventional Algorithm

The first method converts a decimal number to a binary number using a conventional algorithm:
package net.sourcecodeexamples.java.Conversions;

import java.util.Scanner;

/**
 * This class converts a Decimal number to a Binary number
 * 
 * @author https://www.sourcecodeexamples.net
 *
 */
class DecimalToBinary {

    /**
     * Main Method
     *
     * @param args Command Line Arguments
     */
    public static void main(String args[]) {
        conventionalConversion();
    }

    /**
     * This method converts a decimal number
     * to a binary number using a conventional
     * algorithm.
     */
    public static void conventionalConversion() {
        int n, b = 0, c = 0, d;
        Scanner input = new Scanner(System.in);
        System.out.printf("Conventional conversion.%n Enter the decimal number: ");
        n = input.nextInt();
        while (n != 0) {
            d = n % 2;
            b = b + d * (int) Math.pow(10, c++);
            n /= 2;
        } //converting decimal to binary
        System.out.println("\tBinary number: " + b);
        input.close();
    }
}

Output

Conventional conversion.
 Enter the decimal number: 20
 Binary number: 10100

Convert Decimal number to Binary number using a Bitwise Algorithm

The second method converts a decimal number to a binary number using a bitwise algorithm:
package net.sourcecodeexamples.java.Conversions;

import java.util.Scanner;

/**
 * This class converts a Decimal number to a Binary number
 * 
 * @author https://www.sourcecodeexamples.net
 *
 */
class DecimalToBinary {

    /**
     * Main Method
     *
     * @param args Command Line Arguments
     */
    public static void main(String args[]) {
        bitwiseConversion();
    }

    /**
     * This method converts a decimal number
     * to a binary number using a bitwise
     * algorithm
     */
    public static void bitwiseConversion() {
        int n, b = 0, c = 0, d;
        Scanner input = new Scanner(System.in);
        System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
        n = input.nextInt();
        while (n != 0) {
            d = (n & 1);
            b += d * (int) Math.pow(10, c++);
            n >>= 1;
        }
        System.out.println("\tBinary number: " + b);
        input.close();
    }
}

Output

Bitwise conversion.
 Enter the decimal number: 20
 Binary number: 10100
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 50+ Java Programs 



Comments