minahilmumtaz124 minahilmumtaz124
  • 08-03-2022
  • Computers and Technology
contestada

Write a java program to input a number from the user and tell if that number is a power of 2 or not.
Powers of 2 are 1, 2, 4, 8, 16, 32, 64, 128, 256 and so on.

Respuesta :

tonb
tonb tonb
  • 08-03-2022

Answer:

 public static boolean isPowerOfTwo(int n) {

   long exp = Math.round(Math.log(n) / Math.log(2));

   return Math.pow(2, exp) == n;

 }

Explanation:

The opposite of power-of-2 is the 2-log, which is calculated using any log divided by log(2).

I'm sure you can do the input part yourself!

Answer Link

Otras preguntas

ricardo says that if a solid figure has a vertex, it cannot roll. is ricardo correct? explain
a manufacturer produces drinks in small and large bottles. Each small bottle contains s liters of drinks. Each large bottle contains t liters, which is 1 more l
On balance, economic sanctions are reducing the threat russia poses to western interests?
Who is the presiding officer of the senate? Can he/she vote?
What is the y-intercept of the line? y= -2x - 8 y-intercept =
On Serena's tenth birthday,her mom was 3 times Serena's age.How old will Serena and her mom be when her mom's age times 0.5 will equal Serena's age?
why are first hand accounts important?
810 songs 130% increase
turn 7/16 into a percent
Which type of figurative language is used in the following line from John F. Kennedy's inaugural address to the nation? "Now the trumpet summons us again." Symb