Function to find the given number is a power of 2 or not?
Answer Posted / rajiv malhotra
/* THIS SOLUTION IS 100% CORRECT */
#include<stdio.h>
int isPowerOf2(float n)
{
while(n>1)
{
n/=2.0;
}
return (n==1)?1:0; /* 1-> TRUE; 0-> FALSE*/
}
void main()
{
int x,n;
printf("enter a number\n");
fflush(stdin);
scanf("%d",&n);
x=isPowerOf2(n);
if(x==1)
printf("\n yes");
else
printf("\n no");
}
Is This Answer Correct ? | 7 Yes | 2 No |
Post New Answer View All Answers
What are the features of c language?
Why we use break in c?
In a byte, what is the maximum decimal number that you can accommodate?
Explain what are header files and explain what are its uses in c programming?
Which header file should you include if you are to develop a function which can accept variable number of arguments?
What are the advantages of the functions?
"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above
What is the difference between the local variable and global variable in c?
What is the best way to store flag values in a program?
Explain what does a function declared as pascal do differently?
WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?
Why clrscr is used after variable declaration?
What is the best style for code layout in c?
Can you pass an entire structure to functions?
What are the different types of endless loops?