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.

Respuesta :

The solution is in the attachment

Ver imagen fahadisahadam
ijeggs

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();

}

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:

Ver imagen ijeggs

Otras preguntas