Ask Question

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.

+4
Answers (1)
  1. 22 January, 00:06
    0
    The solution code is written in Java.

    public class Main { public static void main (String[] args) { int n = 5; int j; do{ System. out. print ("*"); n--; }while (n > 0); } }

    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already ...” 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