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: