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++
// C++ program for decimal to binary
// conversion using recursion
#include <bits/stdc++.h>
usingnamespacestd;
// Decimal to binary conversion
// using recursion
intfind(intdecimal_number)
{
if(decimal_number == 0)
return0;
else
return(decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver code
intmain()
{
intdecimal_number = 10;
cout << find(decimal_number);
return0;
}
// This code is contributed by shivanisinghss2110
C
// C/C++ program for decimal to binary
// conversion using recursion
#include <stdio.h>
// Decimal to binary conversion
// using recursion
intfind(intdecimal_number)
{
if(decimal_number == 0)
return0;
else
return(decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver code
intmain()
{
intdecimal_number = 10;
printf("%d", find(decimal_number));
return0;
}
Java
// Java program for decimal to binary
// conversion using recursion
importjava.io.*;
classGFG
{
// Decimal to binary conversion
// using recursion
staticintfind(intdecimal_number)
{
if(decimal_number == 0)
return0;
else
return(decimal_number % 2+ 10*
find(decimal_number / 2));
}
// Driver Code
publicstaticvoidmain(String args[])
{
intdecimal_number = 10;
System.out.println(find(decimal_number));
}
}
// This code is contributed by Nikita Tiwari
Python3
# Python3 code for decimal to binary
# conversion using recursion
# Decimal to binary conversion
# using recursion
deffind( decimal_number ):
ifdecimal_number ==0:
return0
else:
return(decimal_number %2+10*
find(int(decimal_number //2)))
# Driver Code
decimal_number =10
print(find(decimal_number))
# This code is contributed
# by "Sharad_Bhardwaj"
C#
// C# program for decimal to binary
// conversion using recursion
usingSystem;
classGFG
{
// Decimal to binary conversion
// using recursion
staticintfind(intdecimal_number)
{
if(decimal_number == 0)
return0;
else
return(decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver Code
publicstaticvoidMain()
{
intdecimal_number = 10;
Console.WriteLine(find(decimal_number));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program for decimal to binary
// conversion using recursion
// Decimal to binary
// conversion using recursion
functionfind($decimal_number)
{
if($decimal_number== 0)
return0;
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.
?>
Javascript
<script>
// Javascript program for decimal to binary
// conversion using recursion
// Decimal to binary conversion
// using recursion
functionfind(decimal_number)
{
if(decimal_number == 0)
return0;
else
return((decimal_number % 2) + 10 *
find(parseInt(decimal_number / 2)));
}
// Driver code
vardecimal_number = 10;
document.write( find(decimal_number));
// This code is contributed by noob2000.
</script>
Output
1010
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!