Monday, August 4, 2014

How to check the given number in the pawor of 2 in java

How to check the given number in the pawor of 2  in java 
 
     import java.util.*;
   public class Check
    {
       public static void main(String args[])
      {
     int num,i,c=0;
  Scanner sc=new Scanner(System.in);
    System.out.println("Enter any number..");
  num=sc.nextInt();
  for(i=0;i<=20;i++)\
   {
      if(num==Math.pow(2,i))
     {
    c=1;
   }
      }
if(c==1)
  System.out.println("The given number in pawor of two  "+num);
else
   System.out.println("The given number not in pawor of two  "+num);
}
}
output:
   Enter any number 10
    The given number not in pawor of two  10
   again execute
   Enter any number 16
   The given number in pawor of two 16

    Explain:
    the pawor of two mean the given number in the form of 2^2,2^3 ,2^4.....
  so 16 is pawor of two  2^4=16.

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...