47.5k views
5 votes
Write a void method printPowers which uses a while loop to print the the first three powers of 1 - 5 as shown below.

1 1 1


2 4 8


3 9 27


4 16 64


5 25 125

User Thomers
by
4.8k points

1 Answer

7 votes

Answer:

class Main {

public static void main(String[] args) {

int n=1;

while(n<=5) {

int power = 1;

while(power <= 3) {

System.out.printf("%d ", (int) Math.pow(n,power));

power++;

}

System.out.println();

n++;

}

}

}

Step-by-step explanation:

A for-loop would lead to simpler code i.m.o.

User Usman Khan
by
4.9k points