Problem: Write a C program to convert a decimal number into its corresponding binary number.

Example:

Binary to Decimal using While Loop

This is the standard way of converting a binary number to its corresponding decimal representation.

Binary to Decimal

We will implement the same as depicted in the above picture i.e multiply each of the binary digits with the value of 2 raised to the power to its index (the position from right – 1 ) and sum them up.

Output

binary to decimal c output

Binary to Decimal using Recursion

The approach used in this method is the same as the above program but implemented using recursion.

Output

binary to decimal c output

In the program we first extract the last digit from the binary number on each recursive call and then we multiply it with its corresponding value of 2 raised to the power its position in the original binary number from the right.

Finally, we sum the multiplication result with the next recursive call result.

Note: Is it important to eliminate the last digit from binary on every recursive call.

In this tutorial we learned to convert a binary number into decimal using c programming language. If you have any doubts or suggestion then please comment below.

Leave a Reply