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

Boiling water models which layer of the Sun best? A. the photosphere B. the radiative zone C. the core D. the convective zone
helllp mee please I really do t understand
why are there fewer christians in northern africa
An oblique hexagonal prism has a base area of 42 square cm. the prism is 4 cm tall and has an edge length of 5 cm.what is the volume of the prism?
A box of shapes contains 3 rectangles, 5 triangles, and 7 hexagons. What is the probability of drawing a triangle or a hexagon? (Note: Percentages are rounded
Find height of cylinder with volume of 271.4 and diameter of 6in
Hey guys csnnu please help asap
What are six different examples of figurative language in the song "Don't Stop Believin' " by Journey. Thanks
Mary Shelley's story of Frankenstein reflects A. her strong desire to discover the origin of life. B. her unusually intense fear of dying. C. the adventures she
Question 9. which describes an str?