109k views
0 votes
BJP4 Exercise 3.2: printPowersOf2

How do you do this????

2 Answers

1 vote

I just looked your problem up and read the directions. Using java:

public class JavaApplication63 {

public static void printPowersOf2(int maximum){

for (int i = 0; i <= maximum; i++){

int num = 1, w = 0;

if (i == 0){

System.out.print(0+" ");

}

else{

while (w < i){

num *= 2;

w++;

}

}

System.out.print(num+" ");

}

}

public static void main(String[] args) {

printPowersOf2(8);

}

}

In my code, we don't use the math class. I hope this helps!

User Pmann
by
5.2k points
6 votes

Answer: 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.

*/

public static void printPowersOf2(int max) {

for (int i = 0; i <= max; i++) {

System.out.print((int) Math.pow(2, i) + " ");

}

System.out.println();

Step-by-step explanation:

User Mofi
by
4.4k points