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

n+16= -13 solve for n
13. What genetic syndrome is shown in this picture? *
How many grams of calcium phosphate does 4.1*10^25 have
What is the maximum height achieved if a 0.400 kg mass is thrown straight upward with an initial speed of 40.0 m⋅s−1? Ignore the effect of air resistance
PLEASE HELP ME ITS KINDA EASY JUST STOP FOR A SECOND
Which graph represents the function f(x) = 4|x|?
Researchers interviewed street prostitutes in Canada and the United States. The mean age of the 100 Canadian prostitutes upon entering prostitution was 19 with
What percentage of migrants to the West were male? 50% 92% OB. 75% OC. 55% OD​
What is this fraction shown above?
How might the context in which he lived affect the philosophy of Confucius? Please help this is due in a few minutes and I really need help: