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