Power of 2
PROBLEM :
Given a positive integer N, check if N is a power of 2.
Input:
The first line contains 'T' denoting the number of test cases. Then follows description of test cases.
Each test case contains a single positive integer N.
Output:
Print "YES" if it is a power of 2 else "NO". (Without the double quotes)
Constraints:
1<=T<=100
0<=N<=10^18
Example:
Input :
2
1
98
Output :
YES
?NO
Explanation: (2^0 == 1)
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
long long t,no,x=2,flag=0 ;
cin>>t ;
while(t--)
{
cin>>no ;
x=2 ;flag=0 ;
if(no==1)
{ cout<<"YES"<<endl ;
flag=1 ;}
else
{
while(x<no)
{
x=x*2 ;
}
if(x==no)
{cout<<"YES"<<endl ;
flag=1 ;}
}
if(flag==0)
cout<<"NO"<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given a positive integer N, check if N is a power of 2.
Input:
The first line contains 'T' denoting the number of test cases. Then follows description of test cases.
Each test case contains a single positive integer N.
Output:
Print "YES" if it is a power of 2 else "NO". (Without the double quotes)
Constraints:
1<=T<=100
0<=N<=10^18
Example:
Input :
2
1
98
Output :
YES
?NO
Explanation: (2^0 == 1)
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
long long t,no,x=2,flag=0 ;
cin>>t ;
while(t--)
{
cin>>no ;
x=2 ;flag=0 ;
if(no==1)
{ cout<<"YES"<<endl ;
flag=1 ;}
else
{
while(x<no)
{
x=x*2 ;
}
if(x==no)
{cout<<"YES"<<endl ;
flag=1 ;}
}
if(flag==0)
cout<<"NO"<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment