-2
In this program I need to count the number 1 present in the binary number after decimal base conversion. Follow the code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
char valor[1000], tmp;
long long int count, valor_i, valor_c = 0, r_div, i, count2;
scanf("%lld", &valor_i);
count = 0;
i = 0;
while (i != 1)
{
r_div = valor_i / 2;
if (r_div < 2)
{
i = 1;
if(valor_i % 2 == 0)
{
valor[count] = '0';
}
else
{
valor[count] = '1';
}
if(r_div == 0)
{
valor[count + 1] = '0';
}
else
{
valor[count + 1] = '1';
}
valor[count + 2] = '\0';
}
else
{
if (valor_i % 2 == 0)
{
valor[count] = '0';
}
else
{
valor[count] = '1';
}
}
count++;
valor_i = r_div;
}
count2 = strlen(valor) - 1;
for(count = 0; count == count2 || count < count2; count++)
{
tmp = valor[count];
valor[count] = valor[count2];
valor[count2] = tmp;
count2--;
}
printf("%s\n", valor);
return 0;
}
What is his problem? This code is not counting the amount of 1 present. That’s the problem?
– Maniero
@bigown is why I think these questions should be closed, all kinds of answers start to come up and you don’t even know what the real problem is.
– Jorge B.
@Jorgeb. That’s why I replied :P since everyone was responding without having much detail, I went on the wave. I think you can infer what they inferred, but it’s not 100% certain that’s what he wants.
– Maniero
@bigown not even 50%. The problem is that people want to win rep and there is nothing wrong, but giving the result will not help the AP.
– Jorge B.
Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).
– Maniero