-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetBitsCounter.c
More file actions
46 lines (39 loc) · 864 Bytes
/
Copy pathSetBitsCounter.c
File metadata and controls
46 lines (39 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
int input(){
//Taking Input
int dec = 0;
printf("Input a decimal number: ");
scanf("%d", &dec);
return dec;
}
long int decToBin(int dec){
//Converting the decimal number to a binary number
long int bin = 0;
int p = 1;
while(dec != 0){
bin += (dec%2)*p;
p *= 10;
dec /= 2;
}
return bin;
}
void output(long int bin, int n){
//Printing the output
int c = 0;
while(bin != 0){
if(bin%10 == 1){
c++;
}
bin /= 10;
}
printf("\nThe count of 1s in binary representation of %d, is: %d\n", n, c);
}
int main(){
//Taking Input
int dec = input();
//Converting the decimal number to a binary number
long int bin = decToBin(dec);
//Printing the output
output(bin, dec);
return 0;
}