172k views
0 votes
Assume 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.

For example, if the value of n is 4, the output should be
*
**
***
****

1 Answer

2 votes

Answer:

public class num6 {

public static void main(String[] args) {

int n = 4;

for(int i = 1; i <= n; ++i) {

for(int j = 1; j <= i; ++j) {

System.out.print("* ");

}

System.out.println();

}

}

}

Step-by-step explanation:

This solution is implemented in Java

Two for loops are required for this (an inner nd outer for loop).

The innner and outer for loops can be seen as implementing rows and columns (so on the first 'row' i=1, so a single star is printed, on the second row, i =2, two stars are printed and so on, all on seperate lines)

User Martijn
by
6.2k points