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 is keyword with example?
what value is returned to operating system after program execution?
Explain the use of keyword 'register' with respect to variables.
What is the difference between scanf and fscanf?
What are the disadvantages of c language?
When do we get logical errors?
What is a keyword?
All technical questions
Can you define which header file to include at compile time?
Define and explain about ! Operator?
What is the explanation for prototype function in c?
Write a program on swapping (100, 50)
What is a const pointer in c?
What is actual argument?
What functions are used in dynamic memory allocation in c?