 # finding a number "p" greater than or equal to any number "n" and has the smallest power of 2.

for example, n=5 so p will be equal to 8 because 8/2=4, 4/2=2
it can’t be 6 because 6/2=3 and i need the smallest power to be 2
I don’t know anything about how i’d go along with this program

Compare with n everytime until the number or acc. to you p , becomes greater than n . Until then multiply the number by 2 using bitshift . For example.

number=number<<1;

int expo= ceil(log(n)/log(2.0));
unsigned long long int ans= pow(2,expo);// Works for 0<n<=2^64

2 simple lines of code We are basically using fact that log(n) to base 2, when rounded up, give an integer K, which is the power to which 2 must be raised.

Basic, Easy to understand approach for this…

int n = given no

int output = 2;

while(output < n){

output = output*2;

}

output variable holds the required answer.

Otherwise you can go with the above technique mentioned by vijju123

Please Upvote and Accept if you find this helpful… This simple code should do the trick considering the fact that log2(n) give the number (float) to which it 2 should be powered for giving the number n i.e 2^(log2(n)) = n.

``````//You should include math.h
Nice and Easy does the trick… 