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)
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes,...
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
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 program that reads in a table of numbers and finds out the average of...
Write a program that reads in a table of numbers and finds out the average of each row and column. This program should first take two numbers as number of rows and columns as input from the user.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT