Java Program to Print Inverted Triangle Star Pattern

  • Java program to print reverse triangle star pattern.
Sample Output,
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Here is the matrix representation of the reverse star pattern. The row numbers are represented by i whereas column numbers are represented by j.

Java program reverse triangle star pattern
Algorithm to print reverse triangle star pattern
  • As the row number increases from top to bottom, number of stars in a row decreases. Let the total number of rows in pattern is R, then the number of stars in Kth row is equal to (R - K + 1). For example,
  • We will use two for loops to print reverse triangle star pattern.
    • For a inverted right triangle star pattern of R rows, outer for loop will iterate R time. Each iteration of outer loop will print one row of the pattern.
    • For Kth row of inverted right triangle pattern, inner loop will iterate (R - K + 1) times. Each iteration of inner loop will print one star character.

Java program to print reverse star pattern

package com.tcc.java.programs;

import java.util.Scanner;

public class StarPatternTwo {
    public static void main(String[] arg) {
        int rows, i, j;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Number of Rows of Pattern");
        rows = scanner.nextInt();

        /* ith row of this row contains rows-i+1 star character */
        for (i = 1; i <= rows; i++) {
            for (j = 1; j <= rows - i + 1; j++) {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
    }
}
Output
Enter Number of Rows of Pattern
7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*