In this tutorial, we will write a program to print an inverted equilateral triangle in java. Before that, you may go through the following topic in java.
It is a Java Program to Print Reverse Pyramid Star Pattern or you can say upside-down equilateral triangle pattern in java.
The program takes the user input for the number of rows to be taken to print the reverse equilateral star pattern.
Example:
1 2 3 4 5 6 7 8 9 | <strong>Input:</strong> number = 7 <strong>Output:</strong> ************* *********** ********* ******* ***** *** * |
Java program to print Inverted Equilateral Star Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, i, j, k; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); num = sc.nextInt(); for (i = num; i > 0; i--) { for (j = 0; j < num - i; j++) { System.out.print(" "); } for (k = 0; k < i; k++) { System.out.print("*" + " "); } System.out.print("\n"); } } } |
Output: