Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number.
Examples of Decimal to Binary:
Input : 7
Output : 111
Input : 10
Output : 1010
Input: 33
Output: 100001
Brute force Approach
For Example:
If the decimal number is 10.
Step 1: Remainder when 10 is divided by 2 is zero. Therefore, arr[0] = 0.
Step 2: Divide 10 by 2. New number is 10/2 = 5.
Step 3: Remainder when 5 is divided by 2 is 1. Therefore, arr[1] = 1.
Step 4: Divide 5 by 2. New number is 5/2 = 2.
Step 5: Remainder when 2 is divided by 2 is zero. Therefore, arr[2] = 0.
Step 6: Divide 2 by 2. New number is 2/2 = 1.
Step 7: Remainder when 1 is divided by 2 is 1. Therefore, arr[3] = 1.
Step 8: Divide 1 by 2. New number is 1/2 = 0.
Step 9: Since number becomes = 0. Print the array in reverse order. Therefore the equivalent binary number is 1010.
The below diagram shows an example of converting the decimal number 17 to an equivalent binary number.

Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
void decToBinary( int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for ( int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
int main()
{
int n = 17;
decToBinary(n);
return 0;
}
|
C
#include <stdio.h>
void decToBinary( int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for ( int j = i - 1; j >= 0; j--)
printf ( "%d" , binaryNum[j]);
}
int main()
{
int n = 17;
decToBinary(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void decToBinary( int n)
{
int [] binaryNum = new int [ 32 ];
int i = 0 ;
while (n > 0 ) {
binaryNum[i] = n % 2 ;
n = n / 2 ;
i++;
}
for ( int j = i - 1 ; j >= 0 ; j--)
System.out.print(binaryNum[j]);
}
public static void main(String[] args)
{
int n = 17 ;
decToBinary(n);
}
}
|
Python3
def decToBinary(n):
binaryNum = [ 0 ] * n;
i = 0 ;
while (n > 0 ):
binaryNum[i] = n % 2 ;
n = int (n / 2 );
i + = 1 ;
for j in range (i - 1 , - 1 , - 1 ):
print (binaryNum[j], end = "");
n = 17 ;
decToBinary(n);
|
C#
using System;
public class GFG {
static void decToBinary( int n)
{
int [] binaryNum = new int [32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for ( int j = i - 1; j >= 0; j--)
Console.Write(binaryNum[j]);
}
public static void Main()
{
int n = 17;
decToBinary(n);
}
}
|
Javascript
<script>
function decToBinary(n)
{
let binaryNum = new Array(32);
let i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = Math.floor(n / 2);
i++;
}
for (let j = i - 1; j >= 0; j--)
document.write(binaryNum[j]);
}
let n = 17;
decToBinary(n);
</script>
|
PHP
<?php
function decToBinary( $n )
{
$binaryNum ;
$i = 0;
while ( $n > 0)
{
$binaryNum [ $i ] = $n % 2;
$n = (int)( $n / 2);
$i ++;
}
for ( $j = $i - 1; $j >= 0; $j --)
echo $binaryNum [ $j ];
}
$n = 17;
decToBinary( $n );
?>
|
Time complexity: O(logn) & Auxiliary Space: O(1)
We can use bitwise operators to do the above job. Note that bitwise operators work faster than arithmetic operators used above.
C++
#include <iostream>
using namespace std;
void decToBinary( int n)
{
for ( int i = 31; i >= 0; i--) {
int k = n >> i;
if (k & 1)
cout << "1" ;
else
cout << "0" ;
}
}
int main()
{
int n = 32;
decToBinary(n);
}
|
C
#include <stdio.h>
int decToBinary( int n)
{
for ( int i = 31; i >= 0; i--) {
int k = n >> i;
if (k & 1)
printf ( "1" );
else printf ( "0" );
}
}
int main()
{
int n = 32;
decToBinary(n);
}
|
Java
class gfg {
public void decToBinary( int n)
{
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 {
public static void main(String[] args)
{
gfg g = new gfg();
int n = 32 ;
g.decToBinary(n);
}
}
|
Python3
def decToBinary(n):
for i in range ( 31 , - 1 , - 1 ):
k = n >> i
if (k & 1 ):
print ( "1" , end = "")
else :
print ( "0" , end = "")
n = 32
decToBinary(n)
|
C#
using System;
class gfg {
public void decToBinary( int n)
{
for ( int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0)
Console.Write( "1" );
else
Console.Write( "0" );
}
}
}
class geek {
public static int Main()
{
gfg g = new gfg();
int n = 32;
g.decToBinary(n);
return 0;
}
}
|
Javascript
<script>
function decToBinary(n)
{
for (i = 31; i >= 0; i--) {
var k = n >> i;
if ((k & 1) > 0)
document.write( "1" );
else
document.write( "0" );
}
}
var n = 32;
decToBinary(n);
</script>
|
PHP
<?php
function decToBinary( $n )
{
for ( $i = 31; $i >= 0; $i --)
{
$k = $n >> $i ;
if ( $k & 1)
echo "1" ;
else
echo "0" ;
}
}
$n = 32;
decToBinary( $n );
?>
|
Output00000000000000000000000000100000
Time complexity: O(1)
loop iterates constant(32) number of times everytime even for small number
Auxiliary Space: O(1)
Efficient Approach
It’s another efficient approach to converting Decimal to binary using the right shift(>>) and And(&) operator. Here we’ll use only Binary Operators which usually are very fast in computation.
C++
#include <bits/stdc++.h>
using namespace std;
string DecimalToBinary( int num)
{
string str;
while (num){
if (num & 1)
str+= '1' ;
else
str+= '0' ;
num>>=1;
}
return str;
}
void reverse(string str)
{
for ( int i=str.size()-1 ; i>=0 ; i--)
cout<< str[i];
}
int main() {
int num = 59;
cout<< "Binary of num 59 is: " ;
reverse( DecimalToBinary(num) );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String DecimalToBinary( int num)
{
String str = "" ;
while (num > 0 ) {
if ((num & 1 ) == 1 )
str += '1' ;
else
str += '0' ;
num >>= 1 ;
}
return str;
}
static void reverse(String str)
{
for ( int i = str.length() - 1 ; i >= 0 ; i--)
System.out.print(str.charAt(i));
}
public static void main(String[] args)
{
int num = 59 ;
System.out.print( "Binary of num 59 is: " );
reverse(DecimalToBinary(num));
}
}
|
Python3
def DecimalToBinary(num):
strs = ""
while num:
if (num & 1 ):
strs + = "1"
else :
strs + = "0"
num >> = 1
return strs
def reverse(strs):
print (strs[:: - 1 ])
num = 59
print ( "Binary of num 59 is:" , end = " " )
reverse(DecimalToBinary(num))
|
C#
using System;
public class GFG
{
public static string DecimalToBinary( int num)
{
string str = "" ;
while (num > 0) {
if ((num & 1) == 1)
str += '1' ;
else
str += '0' ;
num >>= 1;
}
return str;
}
public static void reverse(String str)
{
for ( int i = str.Length - 1; i >= 0; i--)
Console.Write(str[i]);
}
public static void Main( string [] args)
{
int num = 59;
Console.Write( "Binary of num 59 is: " );
reverse(DecimalToBinary(num));
}
}
|
Javascript
<script>
function DecimalToBinary(num)
{
var str = "" ;
while (num)
{
if (num & 1)
str += '1' ;
else
str += '0' ;
num >>= 1;
}
return str;
}
function reverse(str)
{
var res = "" ;
for ( var i = str.length - 1; i >= 0; i--)
res += (str[i]);
document.write(res);
}
var num = 59;
document.write( "Binary of num " + num + " is: " );
reverse(DecimalToBinary(num));
</script>
|
OutputBinary of num 59 is: 111011
Time Complexity: O(log n) & Auxiliary Space: O(1)
Decimal to binary conversion can also be done without using arrays.
C++
#include <cmath>
#include <iostream>
using namespace std;
#define ull unsigned long long int
int decimalToBinary( int N)
{
ull B_Number = 0;
int cnt = 0;
while (N != 0) {
int rem = N % 2;
ull c = pow (10, cnt);
B_Number += rem * c;
N /= 2;
cnt++;
}
return B_Number;
}
int main()
{
int N = 17;
cout << decimalToBinary(N);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
#define ull unsigned long long int
int decimalToBinary( int N)
{
ull B_Number = 0;
int cnt = 0;
while (N != 0) {
int rem = N % 2;
ull c = pow (10, cnt);
B_Number += rem * c;
N /= 2;
cnt++;
}
return B_Number;
}
int main()
{
int N = 17;
printf ( "%u" , decimalToBinary(N));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int decimalToBinary( int N)
{
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 ;
cnt++;
}
return B_Number;
}
public static void main (String[] args)
{
int N = 17 ;
System.out.println (decimalToBinary(N));
}
}
|
Python3
def decimalToBinary(N):
B_Number = 0
cnt = 0
while (N ! = 0 ):
rem = N % 2
c = pow ( 10 , cnt)
B_Number + = rem * c
N / / = 2
cnt + = 1
return B_Number
N = 17
print (decimalToBinary(N))
|
C#
using System;
class GFG
{
static int decimalToBinary( int N)
{
int B_Number = 0;
int cnt = 0;
while (N != 0)
{
int rem = N % 2;
int c = ( int )Math.Pow(10, cnt);
B_Number += rem * c;
N /= 2;
cnt++;
}
return B_Number;
}
static public void Main ()
{
int N = 17;
Console.Write(decimalToBinary(N));
}
}
|
Javascript
<script>
function decimalToBinary(N)
{
var B_Number = 0;
var cnt = 0;
while (N != 0)
{
var rem = N % 2;
var c = Math.pow(10, cnt);
B_Number += rem * c;
N = parseInt(N/2);
cnt++;
}
return B_Number;
}
var N = 17;
document.write(decimalToBinary(N));
</script>
|
Time complexity: O(logn) & Auxiliary Space: O(1)
Note that this method is similar to the one where we convert Binary to Decimal as discussed in this post.
There is yet another method that converts any Decimal Number to its Binary form. The idea is to use bitset.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string decimalToBinary( int n)
{
string s = bitset<64> (n).to_string();
const auto loc1 = s.find( '1' );
if (loc1 != string::npos)
return s.substr(loc1);
return "0" ;
}
int main()
{
int n = 17;
cout << decimalToBinary(n);
return 0;
}
|
Java
import java.util.*;
class DecimalToBinary {
public static String decimalToBinary( int n)
{
String s = Integer.toBinaryString(n);
int loc1 = s.indexOf( "1" );
if (loc1 != - 1 ) {
return s.substring(loc1);
}
return "0" ;
}
public static void main(String[] args)
{
int n = 17 ;
System.out.println(decimalToBinary(n));
}
}
|
Python3
def decimalToBinary( n):
s = bin (n)[ 2 :]
loc1 = s[s.index( '1' ):]
return loc1;
return "0" ;
n = 17 ;
print (decimalToBinary(n));
|
C#
using System;
class HelloWorld {
public static String decimalToBinary( int n)
{
String s = Convert.ToString(n, 2);
return s;
}
static void Main() {
int n = 17;
Console.WriteLine(decimalToBinary(n));
}
}
|
Javascript
function decimalToBinary( n)
{
const s = n.toString(2);
return s;
}
let n = 17;
console.log(decimalToBinary(n));
|
Time complexity: O(logn) & Auxiliary Space: O(1)
Another Approach
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int number = 15;
int n = ( int )(log2(number));
cout << "the binary number is : "
<< bitset<64>(number).to_string().substr(64 - n
- 1);
}
|
Java
import java.util.*;
public class Main{
public static void main(String [] args){
int number = 15 ;
System.out.println( "the binary number is : " + Integer.toString(number, 2 ));
}
}
|
Python3
number = 15
print ( "the binary number is :" , bin (number)[ 2 ::])
|
C#
using System;
class GFG{
public static void Main(){
int number =15;
Console.WriteLine( "the binary number is : " + Convert.ToString(number, 2));
}
}
|
Javascript
var number = 15;
console.log( "the binary number is :" , number.toString(2));
|
Outputthe binary number is : 1111
Time complexity: O(logn) & Auxiliary Space: O(1)