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 :111
Input :10
Output :1010
We have discussed one iterative solution in below post.
Program for Decimal to Binary Conversion
Below is Recursive solution:
findBinary(decimal)
if (decimal == 0)
binary = 0
else
binary = decimal % 2 + 10 * (findBinary(decimal / 2))

Step-by-step process for a better understanding of how the algorithm works
Let the decimal number be 10.
Step 1-> 10 % 2 which is equal-too 0 + 10 * ( 10/2 ) % 2
Step 2-> 5 % 2 which is equal-too 1 + 10 * ( 5 / 2) % 2
Step 3-> 2 % 2 which is equal-too 0 + 10 * ( 2 / 2 ) % 2
Step 4-> 1 % 2 which is equal-too 1 + 10 * ( 1 / 2 ) % 2

Recursion Tree for Decimal to Binary Conversion
C++
#include <bits/stdc++.h>
using namespace std;
int find( int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
int main()
{
int decimal_number = 10;
cout << find(decimal_number);
return 0;
}
|
C
#include <stdio.h>
int find( int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
int main()
{
int decimal_number = 10;
printf ( "%d" , find(decimal_number));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int find( int decimal_number)
{
if (decimal_number == 0 )
return 0 ;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2 ));
}
public static void main(String args[])
{
int decimal_number = 10 ;
System.out.println(find(decimal_number));
}
}
|
Python3
def find( decimal_number ):
if decimal_number = = 0 :
return 0
else :
return (decimal_number % 2 + 10 *
find( int (decimal_number / / 2 )))
decimal_number = 10
print (find(decimal_number))
|
C#
using System;
class GFG
{
static int find( int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
public static void Main()
{
int decimal_number = 10;
Console.WriteLine(find(decimal_number));
}
}
|
PHP
<?php
function find( $decimal_number )
{
if ( $decimal_number == 0)
return 0;
else
return ( $decimal_number % 2 + 10 *
find( $decimal_number / 2));
}
$decimal_number = 10;
echo (find( $decimal_number ));
?>
|
Javascript
<script>
function find(decimal_number)
{
if (decimal_number == 0)
return 0;
else
return ((decimal_number % 2) + 10 *
find(parseInt(decimal_number / 2)));
}
var decimal_number = 10;
document.write( find(decimal_number));
</script>
|
Time Complexity: O(log2n), Here n is the decimal_number.
Auxiliary Space: O(1), As constant extra space is used.
The above approach works fine unless you want to convert a number greater than 1023 in decimal to binary. The binary of 1024 is 10000000000 (one 1 and ten 0’s) which goes out of the range of int. Even with long long unsigned as return type the highest you can go is 1048575 which is way less than the range of int. An easier but effective approach would be to store the individual digits of the binary number in a vector of bool.
C++
#include<bits/stdc++.h>
using namespace std;
void deci_to_bin( int x, string & bin_num)
{
if (x <= 1)
bin_num += ( char )(x + '0' );
else {
deci_to_bin(x / 2, bin_num);
if (x%2)
bin_num += '1' ;
else
bin_num += '0' ;
}
}
int main()
{
string bin_num = "" ;
deci_to_bin(1048576, bin_num);
cout<<bin_num;
return 0;
}
|
Java
public class Main
{
static String bin_num = "" ;
static void deci_to_bin( int x)
{
if (x <= 1 )
bin_num += ( char )(x + '0' );
else {
deci_to_bin(( int )(x / 2 ));
if (x% 2 != 0 )
bin_num += '1' ;
else
bin_num += '0' ;
}
}
public static void main(String[] args) {
deci_to_bin( 1048576 );
System.out.print(bin_num);
}
}
|
Python3
def getbinary(number):
if number = = 0 :
return 0
smallans = getbinary(number / / 2 )
return number % 2 + 10 * smallans
decimal_number = 1048576
print (getbinary(decimal_number))
|
C#
using System;
class GFG {
static string bin_num = "" ;
static void deci_to_bin( int x)
{
if (x <= 1)
bin_num += ( char )(x + '0' );
else {
deci_to_bin(( int )(x / 2));
if (x%2 != 0)
bin_num += '1' ;
else
bin_num += '0' ;
}
}
static void Main() {
deci_to_bin(1048576);
Console.Write(bin_num);
}
}
|
Javascript
<script>
let bin_num = "" ;
function deci_to_bin(x)
{
if (x <= 1)
bin_num += String.fromCharCode(x + '0' .charCodeAt());
else {
deci_to_bin(parseInt(x / 2, 10));
if (x%2 != 0)
bin_num += '1' ;
else
bin_num += '0' ;
}
}
deci_to_bin(1048576);
document.write(bin_num);
</script>
|
Output100000000000000000000
Time Complexity: O(log n), where n is given decimal number
Auxiliary Space: O(log n)