93.7k views
4 votes
Write a public static method called triangle that has one parameter, which is an int variable called rows. The return type for your method should be void. The method should print a triangular shape like the one below. Below is the output where the parameter is 7. You must use for-loops for this problem. You will receive no points for using while or do-while loops. ******* ****** ***** **** *** ** *

User NinjaCross
by
3.2k points

1 Answer

3 votes

Step-by-step explanation:

public class TrainglePattern{

public static void Triangle(int rows)

{

//i=rows-1 (this is to start with the maximum number of stars)

for (int i=rows-1; i>=0; i--)

{

for (int j=rows-i; j>1; j--)

{

System.out.print(" ");

}

//to print the pattern of pyramid

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

{

System.out.print("* ");

}

System.out.println(); //to provide a line break

}

}

public static void main(String []args){

int rows = 7;

Triangle(rows);

}

}

We must note that we must print the triangle pattern with decreasing number of "*". So the initial nested for loop will decide the number space and the second nested for loop will print the "*".

User Misagh Moayyed
by
3.7k points