#include<iostream>
#include<math.h>
using namespace std;
main()
{
int i;
float j=1.00;
cout<<"enter a no"<<endl;
cin>>i;
float k=(float)(log (i)/log(2));
k=k%j;//------------------error-------… "%" cann`t take float vlaue.
if(k==0.00)
cout<<"no "<<i<" is a power of 2"<<endl;
else
cout<<"sorry ! i am afraid "<<i<<" is not a power of 2"<<endl;
}

i tried this but error.
i stuck in one more problem weather a result is integer or not.

this problem can be solved if we find that the result is equivalent to a integer not a floating point no.

#include <iostream>

using namespace std;

int main()
{
	float Num;
	printf( "Enter a number: " );

	cin >> Num;

	if( Num <= 2.0f )
	{
		printf( "Enter number that is bigger than 2" );
		exit( 0 );
	}

	while( 1 )
	{
		if( Num == 2.0f )
		{
			printf( "\nPower of 2 confirmed." );
			break;
		}
		if( ( int )Num <= 0 )
		{
			printf( "\nPower of 2 failed." );
			break;
		}

		Num /= 2;
		printf( "\nNumber: %f", Num );
	}
	getch();
	return 0;
}

shoop


New line saver

bool Power2( float Num )
{
	while( Num != 1.0f )
	{
		if( ( int )Num < 1 )
			return false;
		Num /= 2;
	}
	return true;
}

so if anyone have the logic of finding the result of an expression is equal to integer not in decimal is the answer of the ques. here % dosen`t work as it requires only integer values.

nO loops of any kind you used while loop.

#include <iostream>

using namespace std;

int main()
{
	float Num;
	printf( "Enter a number: " );

	cin >> Num;

	if( Num <= 2.0f )
	{
		printf( "Enter number that is bigger than 2" );
		exit( 0 );
	}

	while( 1 )
	{
		if( Num == 2.0f )
		{
			printf( "\nPower of 2 confirmed." );
			break;
		}
		if( ( int )Num <= 0 )
		{
			printf( "\nPower of 2 failed." );
			break;
		}

		Num /= 2;
		printf( "\nNumber: %f", Num );
	}
	getch();
	return 0;
}

shoop

nO loops of any kind you used while loop.

There is nothing wrong with a loop.
The modulus wouldn't help you anyway because it would only factor if it was evenly divisible by the number, not a power.

NO loops of any kind. you used while loop.

#include <iostream>

using namespace std;

int main()
{
	float Num;
	printf( "Enter a number: " );

	cin >> Num;

	if( Num <= 2.0f )
	{
		printf( "Enter number that is bigger than 2" );
		exit( 0 );
	}

	while( 1 )
	{
		if( Num == 2.0f )
		{
			printf( "\nPower of 2 confirmed." );
			break;
		}
		if( ( int )Num <= 0 )
		{
			printf( "\nPower of 2 failed." );
			break;
		}

		Num /= 2;
		printf( "\nNumber: %f", Num );
	}
	getch();
	return 0;
}

shoop


New line saver

bool Power2( float Num )
{
	while( Num != 1.0f )
	{
		if( ( int )Num < 1 )
			return false;
		Num /= 2;
	}
	return true;
}

ya, i know but if someone asks me "without loop" then i can`t give this answer./

There is nothing wrong with a loop.
The modulus wouldn't help you anyway because it would only factor if it was evenly divisible by the number, not a power.

Just think to yourself this, how does a exponent work.
The same way to build it up is the same way to break it down.

i have a logic for that.
if we find the log of that no (of the base 2), then if the result is equivalent to a integer then the no is of a power of 2.
that`s what i try to do in my original question. so if you can find a result is a integer not a decimal number, then we can solve it without loops.

Just think to yourself this, how does a exponent work.
The same way to build it up is the same way to break it down, if you think otherwise you need to study basic math.

kk, Found this on wiki

(Num - 1 )&Num

The answer is pretty simple.

1. Input a number as an int (as you're doing)
2. Cast the number to double in your log calculation and store the result of the calculation in a double variable.
e.g.

double calcVal = log((double)num)/log(2.0);

3. Create an int version of the result from the above calculation.
e.g.

int calcVal2 = (int)calcVal;

4. Subtract the int from the double, leaving you with the decimal remainder. (0.00003454865..or whatever)
e.g.

double decimal = calcVal - calcVal2;

5. Multiply the decimal remainder by something like 100000 or so and store the final value as an int. This will truncate the later decimal places, from the remainder thereby avoiding the inherent problems with floating point arithmetic.
e.g.

int final = (int)(decimal*1000000);

6. If the final value is 0, then your number is a power of two.

Cheers for now,
Jas.

commented: Nice +2

powerfunction(int i=1,int num)
{
if(i==num || num==1)
return 1;
else if(num>i)
{
i=i<<1;
powerfunction(i,num);
}
else
return 0;
}

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.