Ask Question

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;

+1
Answers (1)
  1. 21 June, 00:40
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers