I wrote a code which is supposed to count how many active bits (1s) are in a binary number that it gets from the user, considering the input is a correct binary number.
every time the code supposed to run the scanf() in main() it just get stuck , it does not stops running, it just feels like its thinking indefinitly and does not give any error
this is the code i wrote which in this situation prints "Please enter a binaric number: " and then it will get stuck
#include <stdio.h>
void count_bits(long int UserNum){
int cnt=0;
while(UserNum>0)
{
if (UserNum%10==1)
{
cnt++;
}
}
printf("there are %d active bits\n",cnt);
}
int main(){
long int UserNum=0;
printf("Please enter a binaric number: ");
scanf("%ld" , &UserNum);
count_bits(UserNum);
return 1;
}
if i write the scanf() first like this it won't even print:
scanf("%ld" , &UserNum);
printf("Please enter a binaric number: ");
what am i doing wrong here?
edit: examples input: 1101100
output:there are 4 active bits
input: 0110100111
output:there are 6 active bits
basically count how many ones there are in the number