Table of contents
1.
Introduction
2.
Examples
3.
Methods for Decimal to Binary Conversion
3.1.
1. Using Arrays
3.2.
2. Using Bitwise Operators
3.3.
3. Using Math.pow() Function (Without Using Arrays)
4.
4: Using Custom Method  
4.1.
How Does It Work? 
4.2.
Java Implementation 
5.
5: Using `toBinaryString()`  
5.1.
How does it work?  
5.2.
Java Implementation
5.3.
Advantages of Using `toBinaryString()`  
5.4.
When to Use This Method  
6.
Steps for Conversion
6.1.
1. Using StringBuilder and Modulo Division
6.2.
2. Using Math.pow() Function
7.
Frequently Asked Questions
7.1.
What is the easiest way to convert a decimal to binary in Java?
7.2.
How does bitwise operation help in decimal to binary conversion?
7.3.
What is the time complexity of decimal to binary conversion?
8.
Conclusion
Last Updated: Mar 23, 2025
Easy

Java Program for Decimal to Binary Conversion

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In programming, converting a decimal number to binary is a fundamental concept. It helps in understanding how computers process and store numbers. Java provides multiple ways to convert a decimal number to its binary equivalent. 

Java Program for Decimal to Binary Conversion

In this article, we will discuss different methods for converting decimal to binary in Java, including arrays, bitwise operators, and mathematical functions. 

Examples

Before diving into different methods, let’s look at an example. If we take the decimal number 10, its binary equivalent is 1010. Another example, decimal 25, is represented as 11001 in binary.

import java.util.Scanner;
public class DecimalToBinaryExample {
    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);
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Enter a decimal number: 10
Binary equivalent: 1010

Methods for Decimal to Binary Conversion

There are multiple ways to convert a decimal number to binary in Java. Below are some of the most commonly used methods.

1. Using Arrays

This method stores binary digits in an array by dividing the decimal number repeatedly by 2.

import java.util.Scanner;

public class DecimalToBinaryArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        int[] binary = new int[40];
        int index = 0;
        while (decimal > 0) {
            binary[index++] = decimal % 2;
            decimal = decimal / 2;
        }
        
        System.out.print("Binary equivalent: ");
        for (int i = index - 1; i >= 0; i--) {
            System.out.print(binary[i]);
        }
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Enter a decimal number: 12
Binary equivalent: 1100

2. Using Bitwise Operators

The bitwise AND (&) and right shift (>>) operators can be used to extract binary digits efficiently.

import java.util.Scanner;
public class DecimalToBinaryBitwise {
    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 = "";    
        while (decimal > 0) {
            binary = (decimal & 1) + binary;
            decimal = decimal >> 1;
        }
        System.out.println("Binary equivalent: " + binary);
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Enter a decimal number: 8
Binary equivalent: 1000

3. Using Math.pow() Function (Without Using Arrays)

This method finds binary digits by dividing the decimal number and using the Math.pow() function to reconstruct the binary equivalent.

import java.util.Scanner;
public class DecimalToBinaryMathPow {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        int binary = 0, i = 0;    
        while (decimal > 0) {
            int remainder = decimal % 2;
            binary += remainder * Math.pow(10, i);
            decimal /= 2;
            i++;
        }
        System.out.println("Binary equivalent: " + binary);
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Enter a decimal number: 7
Binary equivalent: 111

4: Using Custom Method  

Converting a decimal number to binary using a custom method involves breaking down the number step by step. This method is great for understanding the logic behind the conversion process. Let’s walk through it in detail.  

How Does It Work? 

The idea is simple: repeatedly divide the decimal number by 2 & keep track of the remainders. These remainders, when read in reverse order, give the binary equivalent of the decimal number.  

For example, let’s convert the decimal number 10 to binary:  

1. Divide 10 by 2 → Quotient = 5, Remainder = 0  
 

2. Divide 5 by 2 → Quotient = 2, Remainder = 1  
 

3. Divide 2 by 2 → Quotient = 1, Remainder = 0  
 

4. Divide 1 by 2 → Quotient = 0, Remainder = 1  
 

Now, write the remainders in reverse order: 1010. So, the binary equivalent of 10 is 1010.  

Java Implementation 

Here’s how you can implement this logic in Java:  

import java.util.Scanner;  
public class DecimalToBinaryCustom {  
    public static void main(String[] args) {  
        // Create a Scanner object to take user input  
        Scanner scanner = new Scanner(System.in);  
        System.out.print("Enter a decimal number: ");  
        int decimalNumber = scanner.nextInt();  

        // Call the method to convert decimal to binary  
        String binary = convertDecimalToBinary(decimalNumber);  

        // Display the result  
        System.out.println("Binary equivalent: " + binary);  
    }  

    // Method to convert decimal to binary  
    public static String convertDecimalToBinary(int decimalNumber) {  
        // Handle the case for 0 separately  
        if (decimalNumber == 0) {  
            return "0";  
        }  

        // StringBuilder to store the binary result  
        StringBuilder binary = new StringBuilder();  

        // Loop until the decimal number becomes 0  
        while (decimalNumber > 0) {  
            // Append the remainder to the binary string  
            binary.append(decimalNumber % 2);  
            // Divide the decimal number by 2  
            decimalNumber = decimalNumber / 2;  
        }  

        // Reverse the binary string to get the correct result  
        return binary.reverse().toString();  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


Explanation: 

1. Scanner Class: We use `Scanner` to take input from the user.  
 

2. convertDecimalToBinary Method:  

   - A `StringBuilder` is used to store the binary result because it allows efficient string manipulation.  
 

   - The `while` loop runs until the decimal number becomes 0. In each iteration, the remainder of the division by 2 is appended to the `StringBuilder`. 
 

   - Finally, the `StringBuilder` is reversed to get the correct binary representation.  
 

3. Edge Case: If the input is 0, the method directly returns "0".  

5: Using `toBinaryString()`  

Java provides a built-in method called `toBinaryString()` in the `Integer` class, which makes converting decimal numbers to binary extremely easy. This method is efficient & requires minimal code. Let’s discuss how it works.  

How does it work?  

The `toBinaryString()` method takes an integer (decimal number) as input & returns its binary representation as a string. This method handles all the conversion logic internally, so you don’t need to write any additional code for the conversion process.  

For example, if you pass the decimal number 10 to `toBinaryString()`, it will return "1010".  

Java Implementation

Here’s how you can use `toBinaryString()` in Java:  

import java.util.Scanner;  
public class DecimalToBinaryBuiltIn {  
    public static void main(String[] args) {  
        // Create a Scanner object to take user input  
        Scanner scanner = new Scanner(System.in);  
        System.out.print("Enter a decimal number: ");  
        int decimalNumber = scanner.nextInt();  

        // Use Integer.toBinaryString() to convert decimal to binary  
        String binary = Integer.toBinaryString(decimalNumber);  

        // Display the result  
        System.out.println("Binary equivalent: " + binary);  
    }  
}  
You can also try this code with Online Java Compiler
Run Code


 Explanation:

1. Scanner Class: We use `Scanner` to take input from the user.  

2. Integer.toBinaryString():  

   - This method is part of the `Integer` class in Java.  

   - It takes an integer (decimal number) as input & returns its binary representation as a string.  

3. Output: The binary string is directly printed to the console.  

Advantages of Using `toBinaryString()`  

1. Simplicity: You don’t need to write any custom logic for the conversion.  
 

2. Efficiency: The method is optimized & handles all edge cases internally.  
 

3. Readability: The code is clean & easy to understand.  

When to Use This Method  

  • If you need a quick & reliable way to convert decimal to binary without worrying about the underlying logic. 
     
  • When you are working on projects where code simplicity & readability are important. 

Steps for Conversion

1. Using StringBuilder and Modulo Division

  1. Take a decimal number as input.
     
  2. Perform modulo (%) division by 2.
     
  3. Store the remainder (binary digit) in a StringBuilder.
     
  4. Divide the decimal number by 2.
     
  5. Repeat steps 2-4 until the decimal number becomes 0.
     
  6. Reverse the stored binary digits.

2. Using Math.pow() Function

  1. Take a decimal number as input.
     
  2. Extract binary digits using modulo division.
     
  3. Multiply each binary digit by Math.pow(10, i) to construct the binary number.
     
  4. Add the calculated values to obtain the final binary representation.

Frequently Asked Questions

What is the easiest way to convert a decimal to binary in Java?

The easiest way is to use Java’s built-in method Integer.toBinaryString(decimal), which directly converts a decimal number into binary.

How does bitwise operation help in decimal to binary conversion?

Bitwise operations extract binary digits efficiently by using & 1 to get the least significant bit and >> 1 to shift right, reducing execution time.

What is the time complexity of decimal to binary conversion?

The time complexity is O(log n) since each division by 2 reduces the number of digits significantly.

Conclusion

In this article, we covered multiple methods to convert decimal numbers to binary in Java. We discussed using arrays, bitwise operators, and mathematical functions like Math.pow(). Each method has its own advantages, and choosing one depends on the specific requirements of the application. Understanding these conversion techniques is essential for Java programmers, especially for those preparing for technical interviews.

Live masterclass