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

factor the polynomial completely using x method x2+16x+48
The blastocyst generally implants itself to the uterine wall during ______
All women should take a folic acid supplement. A. True B. False
If we have 2 bags containing marbles. bag a has 7 green and 6 black marbles. bag b has 8 yellow and 4 red marbles. if we pick randomly one marble from each bag,
Explain how a third party cookie can track your web browsing across multiple web domains.
A child and sled with a combined mass of 50.0 kg slide down a frictionless hill. if the sled starts from rest and has a speed of 12.0 m/s at the bottom, what is
What did bank customers do following the stock market crash? What impact did this have on many banks?
Hi! I am new and I need help I need to show work and this is for a grade
Which equation is the inverse of y=16x^2 +1
Contraction of the arrector pili would be sensed by