Summary: In this tutorial, we will learn different ways to convert a decimal number into its corresponding binary representation in C programming language.


Method 1: Using While Loop

Binary to Decimal

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

We multiply each binary digit with their corresponding exponential value of 2 (i.e. 2position_from_right -1) and sum them up.

In our program, we use a while loop to extract each binary digit, multiply them with 2x and sum them.


Enter a Binary number:
Decimal: 11

Method 2: Using Recursion

We can also convert a binary number into a decimal using recursion.

In this method, we use the same approach as the above program but implement it in a recursive way.


Enter a Binary number:
Decimal: 5

In each recursive call, we extract a digit from the binary number and 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 programming example, we learned to convert a binary number into decimal in C. If you have any doubts or suggestions then please comment below.

Leave a Reply

4 + 5 =