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

Answer Posted / santhi perumal

#include<stdio.h>
#include<conio.h>

int main()
{
int i,count = 0,number;

printf("Enter the number\n");
scanf("%d",&number);

for(i=15;i>=0;i--)
{
if((1<<i) & number)
count++;
}

if(count == 1)
printf("\nThe Given Number is Power of 2\n");
else
printf("\nThe Given Number is Not Power of 2\n");

}

Is This Answer Correct ?    7 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What the advantages of using Unions?

667


I need previous papers of CSC.......plz help out by posting them.......

1810


Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6)

683


Explain what is output redirection?

661


Who is the main contributor in designing the c language after dennis ritchie?

542






When should I declare a function?

618


.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }

702


What are the rules for identifiers in c?

579


Why is it that not all header files are declared in every C program?

676


How many types of errors are there in c language? Explain

565


What are the complete rules for header file searching?

664


How can I invoke another program (a standalone executable, or an operating system command) from within a c program?

652


How can a program be made to print the line number where an error occurs?

642


What is #include cctype?

573


Add Two Numbers Without Using the Addition Operator

351