Skip to content

Instantly share code, notes, and snippets.

@wohhie
Created July 19, 2016 16:31
Show Gist options
  • Save wohhie/f9769291986fffa3795d96885c8a100a to your computer and use it in GitHub Desktop.
Save wohhie/f9769291986fffa3795d96885c8a100a to your computer and use it in GitHub Desktop.
Decimal To Binary and count number of 1s
#include <stdio.h>
int main() {
int num,
decimal_num,
reminder,
base = 1,
binary = 0,
no_of_1s = 0;
printf("Enter a decimal Number: ");
scanf("%d", &num);
decimal_num = num;
while (num > 0) {
reminder = num % 2;
if (reminder == 1) {
no_of_1s++;
}
binary = binary + reminder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Ints binary number is %d\n", binary);
printf("No.of 1's in the binary is = %d\n", no_of_1s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment