Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method called printPowersOf2 that accepts a maximum number as an argumen

ID: 3557299 • Letter: W

Question

Write a method called printPowersOf2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. For example, consider the following calls: printPowersOf2(3); printPowersOf2(10); These calls should produce the following output: 1 2 4 8 1 2 4 8 16 32 64 128 256 512 1024 You may assume that the value passed to printPowersOf2 is 0 or greater. (The Math class may help you with this problem. If you use it, you may need to cast its results from double to int so that you don't see a .0 after each number in your output. Also, can you write this program without using the Math class?)

Explanation / Answer

First, this is not computer science, it is plain old Java programming.

Second, I assume you know how to write a method, which in your case is called printPowersOf2. This method needs to take an integer argument.

Inside that method, simply use a a loop to walk through and call the Math.pow function. I would suggest a for loop which you can run from 0 to whatever number you passed in and on each loop iteration you print the result of Math.pow(2, loopCounter). loopCounter would need to be replaced by whatever variabel you use in your loop.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote