Ask Question
8 October, 04:51

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;

+2
Answers (1)
  1. 8 October, 08:08
    0
    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 ();

    }

    }

    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Given positive integer numInsects, write a while loop that prints that number doubled without reaching 100. Follow each number with a ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers