C Program to Convert Decimal to Binary | Decimal to Binary in C

C Program to Convert Decimal to Binary


Program to convert a number from decimal to binary can be done using four methods.

 

 

Method 1: Using for loop

Method 2: Using while loop

Method 3: Using Stack

Method 4: Using Array

 

For example, the binary equivalent of decimal number 15 is 1111. The decimal number 15 is recursively divided by 2 until it cannot be divided any further. In each of these cases, the remainder is to be noted. The reverse of the remainder gives the binary equivalent of the decimal number. Let us now look at an example to understand this better.

 

Considering the same example,

15 / 2 = 7, rem = 17 / 2 = 3 , rem = 13 / 2 = 1 , rem = 11 / 2 = 0 , rem = 1

Binary equivalent of 15 is 1111.

decimal to binary

Algorithm

 

  • Input the decimal number.
  • Divide the number by 2 and store the remainder.
  • Repeat step 2 until the number cannot be divided any further
  • Print the remainder in a reversed order.