Question

How can I write in java to find the first power of 2 larger than n,...

How can I write in java to find the first power of 2 larger than n, if n is some number. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class LargerPower2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a value for n: ");
        int n = in.nextInt();
        int p = 1;
        while (p <= n) {
            p *= 2;
        }
        System.out.println("First power of 2 larger than " + n + " is " + p);
    }
}



Add a comment
Know the answer?
Add Answer to:
How can I write in java to find the first power of 2 larger than n,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of...

    Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of some of the testsets. Question 2: Understanding Orders Given an array A of size N, find the number of ordered pairs (i, j) such that i < j and A[i] < A[j]. Input: • First line contains one integer, N, size of array. • Second line contains N space separated integers denoting the elements of the array A. Output: Print the total number of...

  • Write a C++ Program: Larger than n In a program, write a function that accepts three...

    Write a C++ Program: Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Test your function: a user should enter the size of the array, all elements of the array, and a number n.

  • In java, how can I convert this text file format: n=6 m=7 1 2 5 4...

    In java, how can I convert this text file format: n=6 m=7 1 2 5 4 9 5 6 2 3 2 3 4 3 5 6 2 6 4 2 // end of file into this text file format: 1,2,5 1,4,9 1,5,3 2,3,2 3,4,3 5,6,2 6,4,2 This is how to read the first text file: The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers....

    JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers. Write two loops that iterate from 0 through 35. Before each loop, set an integer variable (IV) to 1. Within each loop, print the loop count and the value of IV formatted in two columns. Within the first loop, multiply IV by 2. Within the second loop, multiply IV by the appropriate StrictMath method. The second loop will not complete since there will eventually...

  • How can i code this with in java? - $ java Graph 1 2 3 (...

    How can i code this with in java? - $ java Graph 1 2 3 ( 1 2 3 - Java Graph 11 46 37 牛排 # # # | # # # # # # # # # # # # # # # # # # # | 11 46 37 Note: Spaces should be included to the end of each row.

  • Question 11 How many electrons can have n = 50, I = 2. [Write your answer...

    Question 11 How many electrons can have n = 50, I = 2. [Write your answer as a number.] Not yet answered Answer: Marked out of 2.00 P Flag question

  • Write the code in java programming language To get some practice with recursion. You can do...

    Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

  • Write a program to find the sum of all odd numbers recursively less than or equal...

    Write a program to find the sum of all odd numbers recursively less than or equal to n starting from 1, where n is any positive number. So for example n is 11 find sum of the following numbers (1,3,5,7,9,11) Write this in java please

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT