Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.

Respuesta :

Answer:

The solution code is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        int n = 5;
  4.        int j;
  5.        do{
  6.            System.out.print("*");
  7.            n--;
  8.        }while(n > 0);
  9.    }
  10. }

Explanation:

Firstly, declare the variable n and assign it with value 5 (Line 4). Next declare another variable j.

Next, we create a do while loop to print the n-number of asterisks in one line using print method. In each iteration one "*" will be printed and proceed to the next iteration to print another "*" till the end of the loop. This is important to decrement the n by one before the end of a loop (Line 9) to ensure the loop will only run for n times.