Decimal to binary number using recursion Last Updated : 17 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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: 111Explanation: 20 + 21 + 22 = 1+2+4 = 7.Input: d = 10Output: 1010Explanation: 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. Table of ContentRecursive Approach for Small Integers - O(log2n) Time and O(log2n) Space Recursive Approach for Big Integers Using String - O(log2n) Time and O(log2n) Space Recursive Approach for Small Integers - O(log2n) Time and O(log2n) Space The 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 exampleTo convert 10 to binary10 % 2 = 0, continue with 10 / 2 = 55 % 2 = 1, continue with 5 / 2 = 22 % 2 = 0, continue with 2 / 2 = 11 % 2 = 1, stop as 1 / 2 = 0Reading remainders gives 1010 (binary). C++ #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)); Output1010Recursive Approach for Big Integers Using String - O(log2n) Time and O(log2n) Space 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 a bool vector for scalability. C++ #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); Output100000000000000000000 Comment More infoAdvertise with us Next Article Decimal to binary number using recursion S Shahnawaz_Ali Follow Improve Article Tags : Misc Recursion DSA Basic Coding Problems base-conversion +1 More Practice Tags : MiscRecursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like