Program to Check Whether a Number is Some Power Of 2 (Two):
import java.util.Scanner;
public class Gndec
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,p=0,c=0;
System.out.println("Enter any Number");
a=input.nextInt();
while(a!=1)
{if(a%2!=0)
{ p=1;
System.out.println("No");
break;
}
else{
a=a/2;
}
c++;}
if(p==0)
{
System.out.println("Yes "+c);
}
}
}
Output:
import java.util.Scanner;
public class Gndec
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,p=0,c=0;
System.out.println("Enter any Number");
a=input.nextInt();
while(a!=1)
{if(a%2!=0)
{ p=1;
System.out.println("No");
break;
}
else{
a=a/2;
}
c++;}
if(p==0)
{
System.out.println("Yes "+c);
}
}
}
Output: