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

_____ caused the United States to enter World War I. The revolution in Russia The assassination of Archduke Ferdinand The German attack on Belgium The sink
which statement best explains the relationship between the economy of the south and the north?
What is the ratio of x to y? A. -3/2 B. -2/3 C. 2/3 D. 3/2 (Question attached)
Who said to be or not to be?
A large bin of mixed nuts contains peanuts, cashews, almonds, and walnuts. Luke scoops out a bowl full of nuts and counts how many of each type he has: 18 peanu
Which presidential candidate in 1948 lost the votes of many southern Democrats for his support of civil rights but won the election anyway? A.Dwight Eis
What do scientists estimate is the approximate age of the planet earth?
Solve the equation. q – 29 = –57 (1 point) –28 28 86 –86
Apply for a permanent resident visa- apply for citizenship- get fingerprinted- ?- take oath of allegiance. ( identify the steps of becoming a U.S. citizen)
Where is the field of each charge the strongest ? (A. positive charge arrows pointed out) (B.negative charge arrows pointed inward)