231k views
0 votes
that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.

User Horst
by
5.0k points

2 Answers

0 votes

Complete Question:

Given that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen

Answer:

for(int i = 0; i < n; i++)

{

for(int j = 0; j <= i; j++)

System.out.print("*");

System.out.println();

}

Step-by-step explanation:

To understand this nested for loops, try to visualize i to be number of rows and j to be number of columns

The first for loop runs from i=0 to i<n (assuming n =5) this will ensure five lines (rows)

The second for loop ensures that an asteric (*) is printed for each row length.

see the program output attached below:

that the int variables i and j have been declared, and that n has been declared and-example-1
User Igor Artamonov
by
5.1k points
2 votes

The solution is in the attachment

that the int variables i and j have been declared, and that n has been declared and-example-1
User MQLN
by
4.5k points