Last Updated : 17 Mar, 2025
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: d = 7
Output: 111
Explanation: 20 + 21 + 22 = 1+2+4 = 7.Input: d = 10
Output: 1010
Explanation: 21 + 23 = 2+8 = 10.
We previously discussed an iterative approach in the post Program for Decimal to Binary Conversion, Now, let's focus on the recursive solution.
Recursive Approach for Small Integers - O(log2n) Time and O(log2n) SpaceThe function recursively divides the decimal number by 2, appending the remainder as the next binary digit, constructing the binary representation from right to left.
For example
C++To convert 10 to binary
- 10 % 2 = 0, continue with 10 / 2 = 5
- 5 % 2 = 1, continue with 5 / 2 = 2
- 2 % 2 = 0, continue with 2 / 2 = 1
- 1 % 2 = 1, stop as 1 / 2 = 0
Reading remainders gives 1010 (binary).
#include <bits/stdc++.h>
using namespace std;
// Decimal to binary conversion
// using recursion
int decToBin(int d)
{
if (d == 0)
return 0;
else
return (d % 2 + 10 * decToBin(d / 2));
}
// Driver code
int main()
{
int d = 10;
cout << decToBin(d);
return 0;
}
C
#include <stdio.h>
// Decimal to binary conversion
// using recursion
int decToBin(int d)
{
if (d == 0)
return 0;
else
return (d % 2 + 10 * decToBin(d / 2));
}
// Driver code
int main()
{
int d = 10;
printf("%d", decToBin(d));
return 0;
}
Java
// Decimal to binary conversion
// using recursion
public class DecimalToBinary {
public static int decToBin(int d) {
if (d == 0)
return 0;
else
return (d % 2 + 10 * decToBin(d / 2));
}
// Driver code
public static void main(String[] args) {
int d = 10;
System.out.println(decToBin(d));
}
}
Python
# Decimal to binary conversion
# using recursion
def dec_to_bin(d):
if d == 0:
return 0
else:
return (d % 2 + 10 * dec_to_bin(d // 2))
# Driver code
d = 10
print(dec_to_bin(d))
C#
// Decimal to binary conversion
// using recursion
public class DecimalToBinary {
public static int DecToBin(int d) {
if (d == 0)
return 0;
else
return (d % 2 + 10 * DecToBin(d / 2));
}
// Driver code
public static void Main(string[] args) {
int d = 10;
System.Console.WriteLine(DecToBin(d));
}
}
JavaScript
// Decimal to binary conversion
// using recursion
function decToBin(d) {
if (d === 0)
return 0;
else
return (d % 2 + 10 * decToBin(Math.floor(d / 2)));
}
// Driver code
let d = 10;
console.log(decToBin(d));
Recursive Approach for Big Integers Using String - O(log2n) Time and O(log2n) Space
C++The previous approach fails for numbers above 1023 as their binary representation exceeds the int range. Even using
long long unsigned
is limited to 1048575. A better solution is storing binary digits in abool
vector for scalability.
#include <bits/stdc++.h>
using namespace std;
void decToBinRec(int d, string &res) {
if (d > 1) {
decToBinRec(d / 2, res);
}
res += (d % 2) + '0';
}
string decToBin(int d) {
string res = "";
decToBinRec(d, res);
return res;
}
int main() {
int d = 1048576;
cout << decToBin(d) << endl;
return 0;
}
Java
// Java program to convert decimal to binary using recursion
class DecimalToBinary {
public static String decToBinRec(int d) {
if (d > 1) {
return decToBinRec(d / 2) + (d % 2);
}
return String.valueOf(d);
}
public static void main(String[] args) {
int d = 1048576;
System.out.println(decToBinRec(d));
}
}
Python
def dec_to_bin_rec(d):
if d > 1:
dec_to_bin_rec(d // 2)
return str(d % 2) + (dec_to_bin_rec(d // 2) if d > 1 else '')
def dec_to_bin(d):
return dec_to_bin_rec(d)
if __name__ == '__main__':
d = 1048576
print(dec_to_bin(d))
C#
// C# program to convert decimal to binary using recursion
using System;
class DecimalToBinary {
static string DecToBinRec(int d) {
if (d > 1) {
return DecToBinRec(d / 2) + (d % 2);
}
return d.ToString();
}
static void Main() {
int d = 1048576;
Console.WriteLine(DecToBinRec(d));
}
}
JavaScript
function decToBinRec(d) {
if (d > 1) {
decToBinRec(Math.floor(d / 2));
}
process.stdout.write((d % 2).toString());
}
function decToBin(d) {
decToBinRec(d);
console.log('');
}
const d = 1048576;
decToBin(d);
100000000000000000000
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