Number power of 2

This program is to find out whether the given number is power of 2.

Any number is power of 2 if (num & num -1) =0

Implementation:

#include <stdio.h>

int main()
{
	unsigned int num;
	printf("\n Enter the number: ");
	scanf("%u",&num);

	if((num != 0 && num & num-1) == 0)
		printf("\n The given number %d is a power of Two\n",num);
	else
		printf("\n The given number %d is not a power of Two\n",num);
   return 0;
}

Output:

 Enter the number
: 8

 The given number 8 is a power of Two


Categories: C Language

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 )

Connecting to %s

%d bloggers like this: