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


Decimal to Binary using While Loop

decimal to binary

The approach is the same as depicted in the above diagram.

We need to divide the dividend (i.e decimal) until it reduces to 0 and concatenate and output the remainder (binary) in a bottom-up manner (i.e from last to first).


decimal to binary c output

In the program, we are concatenating each of the upcoming remainders to the left of the binary number.

To concatenate on the left, we multiply equal number of 10 to the remainder as the number of digits already present in the concatenated binary number and then we add it to the concatenated binary number.

Decimal to Binary using Recursion

To convert a decimal into binary we will use ‘Head Recursion’ because ‘Head Recursion’ implements bottom up appraoch.


decimal to binary c output

In this method, we are passing quotient (e.g 15/2 = 7) to each recursive call.

Since, recursion implements stack so the remainder (e.g 15%2 = 1) is getting printed starting from the last recursive call.

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

Leave a Reply