189k views
5 votes
Given positive integer numInsects, write a while loop that prints that number doubled without reaching 100. Follow each number with a space. After the loop, print a newline. Ex: If numInsects = 8, print:

8 16 32 64
import java.util.Scanner;

public class InsectGrowth {
public static void main (String [] args) {
int numInsects = 0;

User Aviemet
by
8.0k points

1 Answer

2 votes

Answer:

import java.util.Scanner;

public class InsectGrowth {

public static void main (String [] args) {

int numInsects = 8;

while(numInsects < 100){

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

numInsects = numInsects *2;

}

System.out.println();

}

}

Step-by-step explanation:

Create the main function and define the variable numInsects with value 8.

Take the while loop and it run until numInsects less than 100.

Inside the loop. first print the value and less double the numInsects value.

for example:

suppose, numInsects = 2

the, check condition 2 < 100, true

print(2)

numInsects = 2* 2=4

second time, numInsects = 4

the, check condition 4 < 100, true

print(4)

numInsects = 4* 2=8

and so on, until numInsects < 100.

Print each number with space and finally when the loop is terminate print the new line.

User Cagin
by
8.2k points