Ask Question
24 December, 03:59

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.

+3
Answers (1)
  1. 24 December, 04:49
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers