Decimal to Binary in Java


In this tutorial, we will learn how to convert a decimal number to binary in Java. We will see 5 different ways to convert decimal to binary in Java.

Decimal and Binary Numbers

Decimal Numbers

A decimal number is a number that is based on the number 10. In our daily use of numbers, we use decimal numbers. For example, 1234 is a decimal number.

A decimal number includes digits from 0 to 9. The decimal number system is also called the base-10 number system.

Binary Numbers

A binary number is a number that is based on the number 2. In the binary number system, we use only 0 and 1. For example, 1010 is a binary number.

A binary number system is also called the base-2 number system. The binary number system is used in computers and other electronic devices.


Decimal to Binary Conversion

We can represent a decimal number in a binary number system. For example:

(0)10 = (0)2
(1)10 = (1)2
(2)10 = (10)2
(5)10 = (111)2
(10)10 = (1010)2
(15)10 = (1111)2
(50)10 = (110010)2
(100)10 = (1100100)2

To convert a decimal number to binary, we need to divide the decimal number by 2. The remainder of the division is the binary digit. We continue dividing the quotient by 2 until the quotient is 0 and write the remainder in reverse order.

decimal to binary conversion
Decimal to Binary Conversion

# Method 1: Print Binary Number Using Array

Here is the first method to convert decimal to binary in Java. In this method, we will use an array to store the remainders.

Algorithm

  1. Take the decimal number as input.
  2. Divide the decimal number by 2 and store the remainder in an array.
  3. Repeat step 2 until the quotient is 0.
  4. Print the array in reverse order.
import java.util.Scanner;

public class DecimalToBinary {
  public static void main(String[] args) {
    // User input
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a decimal number: ");
    int num = sc.nextInt();
    sc.close();

    // Solution

    // initialize an array to store binary number
    int[] binaryNum = new int[100];
    // counter for binary array
    int i = 0;
    while (num > 0) {
      // storing remainder in binary array
      binaryNum[i] = num % 2;
      num = num / 2;
      i++;
    }

    // printing binary array in reverse order
    System.out.print("Binary number: ");
    for (int j = i - 1; j >= 0; j--) {
      System.out.print(binaryNum[j]);
    }
  }
}

Output:

Enter a decimal number: 10
Binary number: 1010

# Method 2: Return Binary Number as String

In this method, we will create a function that will take a decimal number as input and return the binary number as output.

We will return the binary number as a string.

Algorithm

  1. The method takes a decimal number as input.
  2. Initialize an empty string to store the binary number.
  3. Divide the decimal number by 2 and store the remainder in the string. Make sure to add the remainder to the beginning of the string.
  4. Repeat step 3 until the quotient is 0.
  5. Return the binary number as a string.
import java.util.Scanner;

public class DtoB {
  public static String decimalToBinary(int decimal) {
    String binary = "";
    while (decimal > 0) {
      binary = (decimal % 2) + binary;
      decimal = decimal / 2;
    }
    return binary;
  }
  public static void main(String[] args) {
    // User input
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a decimal number: ");
    int num = sc.nextInt();
    sc.close();

    // call the method
    String bin = decimalToBinary(num);
    System.out.println("Binary number: " + bin);
  }
}

Output:

Enter a decimal number: 25
Binary number: 11001

# Method 3: Return Binary Number as Integer Using Array

In this method, we will use an array to store the binary digits. We will reverse the array to get the correct binary number.

import java.util.Scanner;

public class DtoB {
  public static int decimalToBinary(int decimal) {
    int[] binary = new int[100];
    int i = 0;
    while (decimal > 0) {
      binary[i] = decimal % 2;
      decimal = decimal / 2;
      i++;
    }
    int bin = 0;
    for (int j = i - 1; j >= 0; j--) {
      bin = bin * 10 + binary[j];
    }
    return bin;
  }

  public static void main(String[] args) {
    // User input
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a decimal number: ");
    int num = sc.nextInt();
    sc.close();

    // call the method
    int bin = decimalToBinary(num);
    System.out.println("Binary number: " + bin);
  }
}

Output:

Enter a decimal number: 50
Binary number: 110010

Note: Maximum value of an integer in Java is 2,147,483,647. If the binary number is greater than this value, it will give an incorrect output. To increase the maximum value of an integer, we can use the long data type.


# Method 4: Return Binary Number as Integer Without Using Array

In this method, we will create a function that will convert a decimal number to a binary number and return the binary number as an integer.

Binary digits need to reverse to get the correct binary number. So we store each bit in an array and reverse it later.

But in this method, we will not use an array.

import java.util.Scanner;

public class DtoB {
  public static int decimalToBinary(int decimal) {
    int binary = 0;
    int i = 0;
    while (decimal > 0) {
      binary = binary + (decimal % 2) * (int) Math.pow(10, i);
      decimal = decimal / 2;
      i++;
    }
    return binary;
  }
  public static void main(String[] args) {
    // User input
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a decimal number: ");
    int num = sc.nextInt();
    sc.close();

    // call the method
    int bin = decimalToBinary(num);
    System.out.println("Binary number: " + bin);
  }
}

Output:

Enter a decimal number: 100
Binary number: 1100100

# Method 5: Using Recursive Function

In this method, we will use recursion to convert decimal numbers to binary.

A recursive function is a function that calls itself.

import java.util.Scanner;

public class DtoB {
  public static int decimalToBinary(int decimal) {
    if (decimal == 0) {
      return 0;
    } else {
      return (decimal % 2) + 10 * decimalToBinary(decimal / 2);
    }
  }

  public static void main(String[] args) {
    // User input
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a decimal number: ");
    int num = sc.nextInt();
    sc.close();

    // call the method
    int bin = decimalToBinary(num);
    System.out.println("Binary number: " + bin);
  }
}

Output:

Enter a decimal number: 30
Binary number: 11110