Open In App
Related Articles

Java Program to Print Downward Triangle Star Pattern

Improve Article
Improve
Save Article
Save
Like Article
Like

Downward triangle star pattern means we want to print a triangle that is downward facing means base is upwards and by default, orientation is leftwards so the desired triangle to be printed should look like

* * * *
* * *
* *
*

Example

Java




// Java Program to Print Lower Star Triangle Pattern
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Nested 2 for loops for iteration over the matrix
 
        // Outer loop for rows
        int rows = 9;
        for (int a = rows - 1; a >= 0; a--) {
 
            // Inner loop for columns
            for (int b = 0; b <= a; b++) {
 
                // Prints star and space
                System.out.print("*"
                                 + " ");
            }
 
            // By now we are done with single row so new
            // line
            System.out.println();
        }
    }
}


Output

* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Using Recursion

Java




// added by Manish Sharma
 
import java.io.*;
 
class GFG
{   
      public static void printRow(int n)
    {
        if(n == 0) // base case
        {
             return;
        }
 
          System.out.print("* ");
          printRow(n - 1); // next * in the same row
    }
      public static void nextRow(int n)
    {
        if(n == 0)
        {
            return;
        }
          printRow(n); // prints the whole row recursively.
        System.out.print("\n"); // for new line after printing a row...
          nextRow(n - 1);     // changed to new row
    }
   
    public static void main (String[] args)
    {
        GFG.nextRow(5); // no need to create an object of GFG class as nextRow method is static.
    }
}


Output

* * * * * 
* * * * 
* * * 
* * 
* 

Time Complexity: O(n2), where n represents the given number of rows.
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 : 29 Dec, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials