Created
July 19, 2016 16:31
-
-
Save wohhie/f9769291986fffa3795d96885c8a100a to your computer and use it in GitHub Desktop.
Decimal To Binary and count number of 1s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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