Decimal to Binary Conversion in Java

In this article, you will learn how to write the Java logic for Decimal to Binary conversion. The meaning of decimal to binary conversion is the process of converting a number from its decimal representation (base 10) to its binary representation (base 2).

Decimal Number:

In the decimal number system, we use 10 digits (0-9) to represent numbers. Each digit’s position in a decimal number represents a power of 10. For example, in the number 286, the digit 8 is in the tens place, representing 8 * 10^1, and the digit 6 is in the units place, representing 6 * 10^0.

Binary Number:

In the binary number system, we use only 2 digits (0 and 1) to represent numbers. Each digit’s position in a binary number represents a power of 2. The rightmost digit represents 2^0, the next digit to the left represents 2^1, the next digit represents 2^2, and so on.

Decimal to Binary Conversion Process

Converting a decimal number to binary involves dividing the decimal number by 2 repeatedly and recording the remainders until the quotient becomes 0. The remainders are then read in reverse order to obtain the binary representation of the original decimal number.

For example, the decimal number 25 can be converted to binary as follows:

25 divided by 2 gives a quotient of 12 and a remainder of 1
12 divided by 2 gives a quotient of 6 and a remainder of 0
6 divided by 2 gives a quotient of 3 and a remainder of 0
3 divided by 2 gives a quotient of 1 and a remainder of 1
1 divided by 2 gives a quotient of 0 and a remainder of 1

Reading the remainders in reverse order, we get the binary representation: 11001.

Decimal to Binary Conversion Java Programs

Here are a few different solutions for converting a decimal number to its binary equivalent in Java:

1. Using Integer.toBinaryString()

In this solution, the Integer.toBinaryString() method is used to convert the decimal number to its binary representation. The method takes an integer as input and returns a string representing the binary value.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToBinary {

	public static void main(String[] args) {
        
		Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        String binary = Integer.toBinaryString(decimal);
        System.out.println("Binary equivalent: " + binary);
    }

}

OUTPUT:

Enter a decimal number: 25
Binary equivalent: 11001

2. Using a loop and bitwise operations

In this solution, a loop is used to convert the decimal number to binary. The algorithm repeatedly divides the decimal number by 2 and appends the remainder (0 or 1) to a string. The loop continues until the decimal number becomes 0.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToBinary2 {

	public static String convertDecimalToBinary(int decimal) {
		StringBuilder binary = new StringBuilder();

		while (decimal > 0) {
			int remainder = decimal % 2;
			binary.insert(0, remainder);
			decimal = decimal / 2;
		}

		return binary.toString();
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a decimal number: ");
		int decimal = scanner.nextInt();
		String binary = convertDecimalToBinary(decimal);
		System.out.println("Binary equivalent: " + binary);
	}
}

OUTPUT:

Enter a decimal number: 19
Binary equivalent: 10011

3. Using recursion

In this solution, recursion is used to convert the decimal number to binary. The algorithm recursively divides the decimal number by 2 and concatenates the remainder (0 or 1) to the result of the recursive call.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToBinary3 {

	// Recursive method
	public static String convertDecimalToBinary(int decimal) {
		if (decimal == 0) {
			return "0";
		} else if (decimal == 1) {
			return "1";
		} else {
			return convertDecimalToBinary(decimal / 2) + (decimal % 2);
		}
	}

	// main method
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a decimal number: ");
		int decimal = scanner.nextInt();
		String binary = convertDecimalToBinary(decimal);
		System.out.println("Binary equivalent: " + binary);
	}
}

OUTPUT:

Enter a decimal number: 33
Binary equivalent: 100001

Leave a Reply

Your email address will not be published. Required fields are marked *