Program to check whether a number is power of 2 or not.

#include<stdio.h>
#include<math.h>

int pow2(int n)
{ int i;
double t;
for(i=2;i<n/2;i++)
{t=pow(2,i);
if(n==(int)t) return 1;
}
if(i>=n/2) return 0;
}

main()
{int n,p;
printf("Enter a number::\n");
scanf("%d", &n);
p=pow2(n);
if(p==1) printf("The number is power of 2.\n");
else printf("The number is not power of 2.\n");
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s