#include <stdio.h>
int main()
{
int num, count;
count = 0;
num = 8;
while (num != 0)
{
if (num % 2 == 0)
{ // checks if num is even
num = num / 2;
count = count + 1; // increases counter by 1
}
else
{
num = num - 1;
count = count + 1;
}
printf("%d", count); // prints counter
}
return 0;
}
For some reason the output is 1234
instead of 4
, can anyone please explain why? I tried calling scanf()
instead of setting num value to 8 as well, but the output is the same.