Question

JAVA Write a method that finds out if a number is a power of two. Create...

JAVA

Write a method that finds out if a number is a power of two. Create a tester to demonstrate if it works.

Homework Answers

Answer #1
//IsPowerOf2.java
import java.util.Scanner;
public class IsPowerOf2 {
    public static boolean isPowerOfTwo(int n){
        if(n==0)
            return false;

        return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
                (int)(Math.floor(((Math.log(n) / Math.log(2)))));
    }

    // Testing
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = scanner.nextInt();
        if(isPowerOfTwo(n)){
            System.out.println(n+" is a power of 2");
        }
        else{
            System.out.println(n+" is NOT a power of 2");
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
write some lines of Java code that create a Screen object. Then call its clear method...
write some lines of Java code that create a Screen object. Then call its clear method if (and only if) its number of pixels is greater than two million. (Don’t worry about things being logical here; the goal is only to write something that is syntactically correct—i.e., that would compile if we typed it in.)
This is JAVA Write the method findMin that will return the smallest number in an array...
This is JAVA Write the method findMin that will return the smallest number in an array of integers. Examples: findMin({29,2,32,12}) -> 2 public int findMin(int[] numbers) {    }
in java Implement a class Balloon. A balloon starts out with radius 0. Supply a method...
in java Implement a class Balloon. A balloon starts out with radius 0. Supply a method public void inflate(double amount) that increases the radius by the given amount. Supply a method public double getVolume() that returns the current volume of the balloon; volume of a balloon is represented as: V = (4/3) * PI * r^3 Use Math.PI for the value of π. To compute the cube of a value r, you can use Math.pow (https://www.tutorialspoint.com/java/lang/math_pow.htm) or r*r*r Write a...
(Please use java to write these questions) Q1. Create a class on an object Computer with...
(Please use java to write these questions) Q1. Create a class on an object Computer with two fields and two methods as follows: (5 points) field: cpu and memory, method: compile( ) and execute( ) Q2. Write a source code to activate a class created in Q1. (5 points)
a) Write a java statement to import the java utilities. b) Create a Scanner object to...
a) Write a java statement to import the java utilities. b) Create a Scanner object to read input. c) int Age; Write a java statement to read the Age input value.     Write a java statement to read the Age input value.
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
how do i create a java method that takes two ints and divides first int in...
how do i create a java method that takes two ints and divides first int in the parameter by the second int using recursion and nno division or multiplication
Write a Java swap method that could swap values of two integer variables. Do not use...
Write a Java swap method that could swap values of two integer variables. Do not use any predefined method. Also write a method call that swaps the values of the following two variables. int first = 7, second = 5;
Write a program in Java to have a Car class which is comparable. Make the Car...
Write a program in Java to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT