Function to find the given number is a power of 2 or not?

Answer Posted / udhaya

Find if the given number is a power of 2.
//include math.h
void main()
{
int n,logval,powval;
printf("Enter a number to find whether it is s power of
2\n");
scanf("%d",&n);
logval=log(n)/log(2);
powval=pow(2,logval);
if(powval==n)
printf("The number is a power of 2");
else
printf("The number is not a power of 2");
getch();
}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

write a program in C that prompts the user for today's date,tomorrow's date and display the results.Use structures for today's date,tomorrow's date and an array to hold the days for each month of the year.

4991


What is pragma in c?

630


How can you determine the size of an allocated portion of memory?

746


How are Structure passing and returning implemented by the complier?

716


How many bytes is a struct in c?

726






How will you find a duplicate number in a array without negating the nos ?

1649


What is the easiest sorting method to use?

636


Write a simple code fragment that will check if a number is positive or negative.

709


i have a written test for microland please give me test pattern

2183


What are formal parameters?

662


What does & mean in scanf?

604


Explain how can I read and write comma-delimited text?

660


Write a program to generate random numbers in c?

664


When is a void pointer used?

678


Where define directive used?

607