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 is a string?

667


the statement while(i) puts the entire logic in loop. this loop is called a) indefinite loop b) definite loop c) loop syntax wrong d) none of the above

607


program to find error in linklist.(i.e find whether any node point wrongly to previous nodes instead of next node)

1629


process by which one bit patten in to another by bit wise operation is? (a) masking, (b) pruning, (c) biting, (d) chopping,

1893


Can we change the value of #define in c?

587






What is ponter?

775


What is New modifiers?

672


What is the sizeof () a pointer?

550


How to declare pointer variables?

687


What are c preprocessors?

680


Explain how can you restore a redirected standard stream?

593


Explain heap and queue.

589


What does the format %10.2 mean when included in a printf statement?

1091


we need to calculating INCOME TAX for the person. The INCOME TAX is as follows:- First $10000/- of income : 4% tax Next $10000/- of income : 8% tax Next $10000/- of income : 11.5% tax above $10, 00,00/- : 15% tax What is the Solution of this Question ?

804


where are auto variables stored? What are the characteristics of an auto variable?

595