4.4k views
2 votes
Given an int variable n that has already been declared and initialized to a positive value, write a Java program that prints the reverse triangle of asterisks with n asterisks on the first line (top of triangle), one less asterisk on the next level, and so on till 1 asterisk on the bottom level. (All asterisks aligned on the left, not centered in the middle, so you don’t need blanks. But think how you would have solved this problems if you’d need to center the triangle!)

User Maxbareis
by
5.0k points

1 Answer

6 votes

Answer:

The program to this question can be given as:

Program:

public class Main //define class.

{

public static void main(String[] as) //define main method.

{

int n=5; //define variable n and assign positive value.

int i,j; //define variable

for (i = 7; i>=1; i--) //outer loop.

{

for (j = 0; j< i; j++) //inner loop.

{

System.out.print("*"); //print asterisks.

}

System.out.println(); //line break.

}

}

}

Output:

*****

****

***

**

*

Explanation:

The description of the above code can be given as:

  • In the above java programming code firstly we define a class that is "Main". In this class we define a main method in the main method we define a variables that is "n" in this variable we assign a positive value. The we define another integer variable that is i,j this variable is used in the loop.
  • Then we define a loop. for prints asterisks values in reverse triangle, we nested looping concept. In nested looping, we use in loop in this loop we use the i and j variables that print the asterisks value.
User Bnsh
by
5.9k points