// Write a program to check whether a given number is a power of 2
#include<iostream.h>
#include<conio.h>
void main()
{
int n,m,status=1;
clrscr();
cout<<"Enter a number: ";
cin>>n;
m=n;
while(n>2)
if(n%2==1)
{
status=0;
break;
}
else
n=n/2;
if(status)
cout<<m<<" is power of 2";
else
cout<<m<<" is not power of 2";
getch();
}

No comments: