Showing posts with label checks number is power of two. Show all posts
Showing posts with label checks number is power of two. Show all posts

Sunday, 15 May 2016

Java code to check whether a number is power of two


import java.util.Scanner;

public class CheckPowerTwo {
   
     
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
   
    for( i=0;i<a;i++)
    {
    if(a==Math.pow(2,i))
    { System.out.println("The number is power of two");break;
    }
   
    }
    if(i==a)
        System.out.println("The number is not power of two");
   
   
    }
   
}