How to Convert Decimal to Binary Using Recursion in Python?

In this article, we will show you how to convert decimal to binary using recursion in python.

A decimal number is the most familiar number system to the general public. It is base 10 which has only 10 symbols ? 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Whereas Binary number is the most familiar number system to digital systems, networking, and computer professionals. It is base 2 which has only 2 symbols: 0 and 1, these digits can be represented by off and on respectively.

When we convert a number from the decimal number system to the binary number system, we are using decimal to binary-conversion. The total number of digits used in the number system determines the base of all number systems. The binary number system, for example, has a base of two because it only uses two digits to represent a number. Similarly, the decimal number system has a base of ten because a number is represented by ten digits.

Using Recursion(First Logic)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task ?

  • Create a recursive function to getBinaryForm() using the def keyword to convert the decimal number passed to it as an argument into a binary form.

  • Use the if conditional statement to check whether the number passed is equal to 0 with the == operator.

  • Return 0 if the condition is true i.e, the decimal number passed is 0.

  • Else return the binary form of the decimal number passed to the function using recursive logic( get the last bit of the number using the modulus operator(%) and divide the number by 2(half) and multiply it with 10 and Cal the recursive function again with this value-added).

  • Create a variable to store the input number.

  • Call the getBinaryForm() function by passing the input decimal number as an argument and print the resultant binary equivalent of the decimal number returned by the function.

Example

The following program returns the binary form of a decimal number using recursion ?

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function to convert decimal number passed to it</span>
<span class="token comment"># as an argument into a binary form</span>
<span class="token keyword">def</span> <span class="token function">getBinaryForm</span><span class="token punctuation">(</span>decimalnum<span class="token punctuation">)</span><span class="token punctuation">:</span>

<span class="token comment"># checking whether the number passed is equal to 0</span>
   <span class="token keyword">if</span> decimalnum <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
   <span class="token comment"># returning 0 if the number passed is 0</span>
      <span class="token keyword">return</span> <span class="token number">0</span>
   <span class="token keyword">else</span><span class="token punctuation">:</span>
   <span class="token comment"># Else getting the last bit of the number and dividing the number by 2(half) and multiplying it with 10</span>
   <span class="token comment"># Calling the recursive function again with this value-added</span>
      <span class="token keyword">return</span> <span class="token punctuation">(</span>decimalnum <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> getBinaryForm<span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>decimalnum <span class="token operator">//</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># input decimal number</span>
decimalnum <span class="token operator">=</span> <span class="token number">5</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The binary equivalent of"</span><span class="token punctuation">,</span>decimalnum<span class="token punctuation">,</span><span class="token string">"is:"</span><span class="token punctuation">)</span>
<span class="token comment"># calling the getBinaryForm() function by passing the decimal number as an argument</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>getBinaryForm<span class="token punctuation">(</span>decimalnum<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output &minnus;

The binary equivalent of 5 is:
101

Using Recursion(Second Logic)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task ?

  • Create a recursive function to getBinaryForm() using the def keyword to convert the decimal number passed to it as an argument into a binary form

  • Use the if conditional statement to check whether the number passed is equal to 0 with the == operator.

  • Return 0 if the condition is true i.e, the decimal number passed is 0.

  • Call the function recursively again by passing the given number by half and store this result in a variable.

  • Get the last bit of the given decimal number using the modulus operator(%) and add 10* to the above result.

  • Create a variable to store the input number.

  • Call the getBinaryForm() function by passing the input decimal number as an argument and print the resultant binary equivalent of the decimal number returned by the function.

Example

The following program returns the binary form of a decimal number using recursion ?

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function to convert decimal number passed to it</span>
<span class="token comment"># as an argument into a binary form</span>
<span class="token keyword">def</span> <span class="token function">getBinaryForm</span><span class="token punctuation">(</span>decimalnum<span class="token punctuation">)</span><span class="token punctuation">:</span>

   <span class="token comment"># checking whether the number passed is equal to 0</span>
   <span class="token keyword">if</span> decimalnum <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
   <span class="token comment"># returning 0 if the number passed is 0</span>
      <span class="token keyword">return</span> <span class="token number">0</span>
   <span class="token comment"># Call the function recursively again by passing the given number by half</span>
   result <span class="token operator">=</span> getBinaryForm<span class="token punctuation">(</span>decimalnum <span class="token operator">//</span> <span class="token number">2</span><span class="token punctuation">)</span>
   <span class="token comment"># Getting the last bit and multiply the result with 10</span>
   <span class="token keyword">return</span> decimalnum <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> result

<span class="token comment"># input decimal number</span>
decimalnum <span class="token operator">=</span> <span class="token number">500</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The binary equivalent of"</span><span class="token punctuation">,</span>decimalnum<span class="token punctuation">,</span><span class="token string">"is:"</span><span class="token punctuation">)</span>

<span class="token comment"># calling the getBinaryForm() function by passing</span>
<span class="token comment"># the decimal number as an argument</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>getBinaryForm<span class="token punctuation">(</span>decimalnum<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The binary equivalent of 500 is:
111110100

Conclusion

We learned two different methods for calculating the binary format of a given decimal number using recursion in this article. We learned how to invoke the recursive function by passing it some value (result). We also learned how to divide a number by half to get only the integer number.

Updated on: 2022-10-27T12:37:31+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements