Decimal To Binary Conversion | C Program

Decimal to binary conversion in C:-

Here, in this page we will discuss decimal to binary conversion in C.The C program to convert decimal number into binary number is done by counting the number of 1s. The program practices module process and multiplication with base 2 operation for converting decimal into binary number. It further uses modulo operation to check for 1’s and hence increases the amount of 1s. The program reads an integer number in decimal in order to change or convert into binary number.

For conversion, we divide the decimal number by 2 and get the quotient for the next iteration and continuously store the remainder value in an array in reverse order till the decimal number is greater than 0. For better understanding with an example just have a look towards the diagram shown at the right.

Ex:- (5)10 in Base 10 (Decimal number) =(101)2 in base 2(Binary number)

Decimal to binary



  • Take a decimal number from the user and store it in variable say number.
  • Create a variable say dec_num and initialize it with number, declare one variable say bin and initialize it with zero.
  • Run a loop till number is greater than zero.
  • Calculate the remainder when number is divide by 2 and add that remainder to variable bin and divide the number by 2.
  • The above step will continue till number(decimal number is greater than 0).
  • Print the bin value as it contains the converted value.

Program to convert a decimal number into binary number:

 * C program to accept a decimal number and convert it to binary
 * and count the number of 1's in the binary number

#include<stdio.h> int main() {
//for initialize a variables     long number, dec_num, rem, base = 1, bin = 0, count = 0;
//To insert a number     printf("Insert a decimal num \n");     scanf("%ld", &number);     dec_num = number;     while(number > 0)     {         rem = number % 2;         /*  To count no.of 1s */         if (rem == 1)         {             count++;         }
        bin = bin + rem * base;
//number/=2;         number = number / 2;         base = base * 10;     }
//display     printf("Input num is = %d\n", dec_num);     printf("Its binary equivalent is = %ld\n", bin);     printf("Num of 1's in the binary num is = %d\n", count); return 0;


Insert a decimal num: 134
Input num is: 134
Its binary equivalent is 10000110
Num of 1’s in the binary num is 3