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


Please Help Members By Posting Answers For Below Questions

What are the features of c language?

617


Why we use break in c?

543


In a byte, what is the maximum decimal number that you can accommodate?

618


Explain what are header files and explain what are its uses in c programming?

621


Which header file should you include if you are to develop a function which can accept variable number of arguments?

797






What are the advantages of the functions?

600


"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

605


What is the difference between the local variable and global variable in c?

524


What is the best way to store flag values in a program?

576


Explain what does a function declared as pascal do differently?

631


WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?

1853


Why clrscr is used after variable declaration?

1033


What is the best style for code layout in c?

626


Can you pass an entire structure to functions?

686


What are the different types of endless loops?

615