Decimal to binary number using recursion - GeeksforGeeks

06 Dec, 2021
Given a decimal number as input, we need to write a program to convert the given decimal number into 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 ConversionBelow is Recursive solution:
findBinary(decimal)
   if (decimal == 0)
      binary = 0
   else
      binary = decimal % 2 + 10 * (findBinary(decimal / 2)
Step by step process for better understanding of how the algorithm works Let 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) % 2Step 3-> 2 % 2 which is equal-too 0 + 10 * ( 2 / 2 ) % 2Step 4-> 1 % 2 which is equal-too 1 + 10 * ( 1 / 2 ) % 2
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program for decimal to binary// conversion using recursion#include <bits/stdc++.h>using namespace std; // Decimal to binary conversion// using recursionint find(int decimal_number){    if (decimal_number == 0)        return 0;    else        return (decimal_number % 2 + 10 *                find(decimal_number / 2));} // Driver codeint main(){    int decimal_number = 10;    cout << find(decimal_number);    return 0;}// This code is contributed by shivanisinghss2110
// C/C++ program for decimal to binary// conversion using recursion#include <stdio.h> // Decimal to binary conversion// using recursionint find(int decimal_number){    if (decimal_number == 0)        return 0;    else        return (decimal_number % 2 + 10 *                find(decimal_number / 2));} // Driver codeint main(){    int decimal_number = 10;    printf("%d", find(decimal_number));    return 0;}
// Java program for decimal to binary// conversion using recursionimport java.io.*; class GFG{         // Decimal to binary conversion    // using recursion    static int find(int decimal_number)    {        if (decimal_number == 0)            return 0;                     else                 return (decimal_number % 2 + 10 *                find(decimal_number / 2));    }     // Driver Codepublic static void main(String args[]){    int decimal_number = 10;    System.out.println(find(decimal_number));} } // This code is contributed by Nikita Tiwari
# Python3 code for decimal to binary# conversion using recursion # Decimal to binary conversion# using recursiondef find( decimal_number ):    if decimal_number == 0:        return 0    else:        return (decimal_number % 2 + 10 *                find(int(decimal_number // 2))) # Driver Codedecimal_number = 10print(find(decimal_number)) # This code is contributed# by "Sharad_Bhardwaj"
// C# program for decimal to binary// conversion using recursionusing System; class GFG{         // Decimal to binary conversion    // using recursion    static int find(int decimal_number)    {        if (decimal_number == 0)            return 0;                     else                 return (decimal_number % 2 + 10 *                find(decimal_number / 2));    }         // Driver Code    public static void Main()    {                 int decimal_number = 10;                 Console.WriteLine(find(decimal_number));    }} // This code is contributed by vt_m
<?php// PHP program for decimal to binary// conversion using recursion // Decimal to binary// conversion using recursionfunction find($decimal_number){    if ($decimal_number == 0)        return 0;    else        return ($decimal_number % 2 + 10 *                find($decimal_number / 2));} // Driver Code$decimal_number = 10;echo(find($decimal_number)); // This code is contributed by Ajit.?>
<script> // Javascript program for decimal to binary// conversion using recursion // Decimal to binary conversion// using recursionfunction find(decimal_number){    if (decimal_number == 0)        return 0;    else        return ((decimal_number % 2) + 10 *                find(parseInt(decimal_number / 2)));} // Driver codevar decimal_number = 10;document.write( find(decimal_number)); // This code is contributed by noob2000.</script>
1010
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++
Java
Python3
C#
Javascript
// C++ program for decimal to binary// conversion using recursion#include<bits/stdc++.h>using namespace std;  // Function to convert decimal to binaryvoid deci_to_bin(int x, string & bin_num){       // Base Case    if (x <= 1)        bin_num += (char)(x + '0');    else {               // Recursion call        deci_to_bin(x / 2, bin_num);               // If x is divisible by 2        if(x%2)          bin_num += '1';               // otherwise        else          bin_num += '0';    }} // Driver Codeint main(){    string bin_num = "";    deci_to_bin(1048576, bin_num);         cout<<bin_num;    return 0;}
// Java program for decimal to binary// conversion using recursionpublic class Main{    static String bin_num = "";           // Function to convert decimal to binary    static void deci_to_bin(int x)    {           // Base Case        if (x <= 1)            bin_num += (char)(x + '0');        else {               // Recursion call            deci_to_bin((int)(x / 2));               // If x is divisible by 2            if(x%2 != 0)              bin_num += '1';               // otherwise            else              bin_num += '0';        }    }          public static void main(String[] args) {        deci_to_bin(1048576);                System.out.print(bin_num);    }} // This code is contributed by divyesh072019.
# Python3 code for decimal to binary# conversion using recursion  # Decimal to binary conversion# using recursiondef getbinary(number):       # Base case    if number == 0:        return 0            # Recursion call and storing the result    smallans = getbinary(number // 2)         return number % 2 + 10 * smallans   # Driver Codedecimal_number = 1048576print(getbinary(decimal_number))  # This code is contributed# by "Sarthak Sethi"
// C# program for decimal to binary// conversion using recursionusing System;class GFG {         static string bin_num = "";          // Function to convert decimal to binary    static void deci_to_bin(int x)    {          // Base Case        if (x <= 1)            bin_num += (char)(x + '0');        else {              // Recursion call            deci_to_bin((int)(x / 2));              // If x is divisible by 2            if(x%2 != 0)              bin_num += '1';              // otherwise            else              bin_num += '0';        }    }       static void Main() {    deci_to_bin(1048576);           Console.Write(bin_num);  }} // This code is contributed by mukesh07.
<script>    // Javascript program for decimal to binary    // conversion using recursion         let bin_num = "";         // Function to convert decimal to binary    function deci_to_bin(x)    {         // Base Case        if (x <= 1)            bin_num += String.fromCharCode(x + '0'.charCodeAt());        else {             // Recursion call            deci_to_bin(parseInt(x / 2, 10));             // If x is divisible by 2            if(x%2 != 0)              bin_num += '1';             // otherwise            else              bin_num += '0';        }    }         deci_to_bin(1048576);          document.write(bin_num);         // This code is contributed by divyeshrabadiya07.</script>
100000000000000000000
jit_t
mohnishsrikanth
shivanisinghss2110
yugal1407
ersarthaksethi
noob2000
divyeshrabadiya07
mukesh07
divyesh072019
base-conversion
Recursion
School Programming
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Backtracking | Introduction
Print all possible combinations of r elements in a given array of size n
Recursive Practice Problems with Solutions
Write a program to reverse digits of a number
Recursive Functions
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java