Last Updated : 23 Jul, 2025
Try it on GfG Practice
Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number.
Examples:
Input : 7
Output : 111Input: 33
Output: 100001
Binary-to-decimal conversion is done to convert a number given in the binary system to its equivalent in the decimal number system. A number system is a format to represent numbers in a certain way.
Methods For Decimal to Binary ConversionBinary Number System - The binary number system is used in computers and electronic systems to represent data, and it consists of only two digits which are 0 and 1.
Decimal Number System - The decimal number system is the most commonly used number system worldwide, which is easily understandable to people. It consists of digits from 0 to 9.
There are numerous approaches to converting the given decimal number into an equivalent binary number in Java. A few of them are listed below.
- Store the remainder when the number is divided by 2 in an array.
- Divide the number by 2
- Repeat the above two steps until the number is greater than zero.
- Print the array in reverse order now.
The below diagram shows an example of converting the decimal number 17 to an equivalent binary number.
Java Program to Convert Decimal Number to Binary Using Arrays Java
// Java program to convert a decimal
// number to binary number
import java.io.*;
class GFG
{
// function to convert decimal to binary
static void decToBinary(int n)
{
// array to store binary number
int[] binaryNum = new int[1000];
// counter for binary array
int i = 0;
while (n > 0)
{
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
System.out.print(binaryNum[j]);
}
// driver program
public static void main (String[] args)
{
int n = 17;
System.out.println("Decimal - " + n);
System.out.print("Binary - ");
decToBinary(n);
}
}
Decimal - 17 Binary - 10001The complexity of the above method:
2. Using Bitwise OperatorsTime Complexity: O(log2(n))
Auxiliary Space: O(1000)
We can use bitwise operators to do the above job.
Java Program to Convert Decimal Number to Binary Using Bitwise Operators JavaNote - Bitwise operators work faster than arithmetic operators used above.
// Java program to Decimal to binary conversion
// using bitwise operator
// Size of an integer is assumed to be 32 bits
class gfg {
// Function that convert Decimal to binary
public void decToBinary(int n)
{
// Size of an integer is assumed to be 32 bits
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0)
System.out.print("1");
else
System.out.print("0");
}
}
}
class geek {
// driver code
public static void main(String[] args)
{
gfg g = new gfg();
int n = 32;
System.out.println("Decimal - " + n);
System.out.print("Binary - ");
g.decToBinary(n);
}
}
Decimal - 32 Binary - 00000000000000000000000000100000The complexity of the above method:
3. Using Math.pow() method (Without using Arrays)Time Complexity: O(1)
Auxiliary Space: O(1)
Decimal to binary conversion can also be done without using arrays.
Java Program to Convert Decimal Number to Binary Using Math.pow() Method Java
// Java implementation of the approach
import java.io.*;
class GFG {
// Function to return the binary
// equivalent of decimal value N
static int decimalToBinary(int N)
{
// To store the binary number
int B_Number = 0;
int cnt = 0;
while (N != 0) {
int rem = N % 2;
double c = Math.pow(10, cnt);
B_Number += rem * c;
N /= 2;
// Count used to store exponent value
cnt++;
}
return B_Number;
}
// Driver code
public static void main(String[] args)
{
int N = 17;
System.out.println("Decimal - " + N);
System.out.print("Binary - ");
System.out.println(decimalToBinary(N));
}
}
// This code is contributed by ajit.
Decimal - 17 Binary - 10001The complexity of the above method
Steps for ConversionTime Complexity: O(log n)
Auxiliary Space: O(1)
public class GFG {
// Main driver method
public static void main(String[] args)
{
int decimal = 10;
String binary = decimalToBinary(decimal);
System.out.println("Decimal: " + decimal);
System.out.println("Binary: " + binary);
}
public static String decimalToBinary(int n)
{
int remainder, quotient = n;
String binaryNum = "";
while (quotient > 0) {
remainder = quotient % 2;
binaryNum
= Integer.toString(remainder) + binaryNum;
quotient = quotient / 2;
}
return binaryNum;
}
}
Decimal: 10 Binary: 1010The complexity of the above method
Time Complexity: O(log n)
Auxiliary Space: O(log n)
Please refer complete article on Program for Decimal to Binary Conversion for more details!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4