Open In App
Related Articles

Java Program to Print Upper Star Triangle Pattern

Improve Article
Save Article
Like Article

The upper star triangle pattern means the base has to be at the bottom and there will only be one star to be printed in the first row. Here is the issue that will arise is what way we traverse be it forward or backward we can’t ignore the white spaces, so we are not able to print a star in the first row using only two nested for loops. The answer to this is very simple as we will break this problem into two halves, we will be running two nested for loops, one managing white spaces and the other managing pattern printing rest remains the same.



// Java Program to  Print Upper Star Triangle Pattern
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
        // Declaring and initializing variable representing
        // number of rows to be printed
        int k = 9;
        // Nested 2 for loops for iterating over the matrix
        // Outer for loop for iterating over rows
        for (int a = 0; a <= k; a++) {
            // Inner for loop for iterating over columns
            // where we are printing white spaces
            for (int b = 1; b <= k - a; b++) {
                // Print the white space
                System.out.print(" ");
            // Inner for loop for iterating over columns
            // where we are printing white spaces
            for (int l = 0; l <= a; l++) {
                // Print the star pattern
            // By now we are done with one row so
            // next line



Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 Oct, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials