int log2(int n)
{
   int count = 1;
   
   while(!n >= 1)
   {
            if (n%2 == 0)
            {
            count++;
            n=n/2;
            }
            else
            {
            count++;
            n=n/2;
            floor(n);
            }
   
   }
   
   return count;
}

not sure why it isnt working?

Can you tell me what output you have and why you don't like it ?

I've changed your code a bit:

int log2(int n)
{
   int count = 1;
   
   while(n >= 1)
   {
	if (n%2 == 0)
	{
		count++;
		n=n/2;
	} else {
                count++;
                n=n/2;
                floor(n);
	}
   }
   
   return count-2;
}

I made changes to line 5 and to line 18 ...
On line 5 you're saying: while(!n >=1) well, actually the while is never run for values higher than or equal to 1 ...
Translated to human words: repeat this code IF n is NOT higher than 1 OR IF n is NOT equal to 1 ...
==> If you're dealing with values higher or equal to 1, the loop is never run as a result ...
Solution: Remove the '!'-sign and it works ...

In line 18: return count-2; because it was always returning values which were 2 higher than the actual solution ...

Hope this helps !

thanks dude it works! have a good one! xo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.