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

Why is it important to write something in the subject line of emails?
What effects of long-term stress would include lowered immunity, heart disease, and obesity
Why were western powers interested in countries in the Middle East, like Iran and Saudi Arabia? A. Democratic nations felt threatened by Islamic nations. B.
6279 divided by 7 I hope someone can help!​
Unlike W.E.B. DuBois and Booker T. Washington, Marcus Garvey A) was associated with a "back to Africa" movement for African Americans. B) believed that African
In American democracy, why is the principle of checks and balances important? A. Elections are fair and free B. Minority rights are protected C. No one branch c
In quadrilateral QRST, angle RST measures (5x + 15)degrees. Angle TQR measures (4x + 3)degrees.What is the measure of angle RST
Explain how the discount rate affects the interest rate charged to consumers
Determine the type of boundary line and shading for the graph of the inequality 4x + y > −6.
Is there less 1 million, exactly 1 million or greater than 1 million milligrams in 1 kilogram? Explain what u know. ?