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

Respuesta :

tonb

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++;

   }

 }

}

Explanation:

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