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

The perimeter of a rectangle is 36 inches. If the width of the rectangle is 6 inches, what is the length? A.30 inches B.24 inches C.18 inches D.12 inches
Is three fourths equal to five sixths?
Which best describes the conquest of Mexico? a. Led by Hernandez Cortez, Spanish armies seized the Aztec emperor and laid siege to the capital city. b. Through
What is -5x minus x? This one's a tricky one for me. The question is (-5) - x - 10. Thanks if you answer!
what happens when a team wins the serve in volleyball
How to turn a fraction into a decimal
What is 179.3 divided by 61.5 please show work
In which way did Georgia's success with agriculture directly affect its population of slaves
According to the Declaration of Independence, when do the people have the right to overthrow their government?
Which of the following bacterial cells would best be able to survive an attack by an antibiotic? (2 points) A bacterial cell that used its toxins to alter its
good job