C Programming Code Examples C > Conversions Code Examples Program to Convert a Decimal Number to Binary & Count the Number of 1s Program to Convert a Decimal Number to Binary & Count the Number of 1s This C Program converts a decimal number into binary & count the number of 1s. The program uses module operation and multiplication with base 2 operation for conversion. It also uses modulo operation to check for 1's and accordingly increments the count of 1s. /* - 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> void main() { long num, decimal_num, remainder, base = 1, binary = 0, number-of-1s = 0; printf("Enter a decimal integer \n"); scanf("%ld", &num); decimal_num = num; while (num > 0) { remainder = num % 2; /* To count no.of 1s */ if (remainder == 1) { number-of-1s++; } binary = binary + remainder * base; num = num / 2; base = base * 10; } printf("Input number is = %d\n", decimal_num); printf("Its binary equivalent is = %ld\n", binary); printf("No.of 1's in the binary number is = %d\n", number-of-1s); }