Open In App

Java Program to Print Pyramid Star Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

This article will guide you through the process of printing a Pyramid star pattern in Java.

1. Simple pyramid pattern

Java




import java.io.*;
 
// Java code to demonstrate Pyramid star patterns
public class GeeksForGeeks {
    // Function to demonstrate printing pattern
    public static void PyramidStar(int n)
    {
        int a, b;
 
        // outer loop to handle number of rows
        // k in this case
        for (a = 0; a < n; a++) {
 
            // inner loop to handle number of columns
            // values changing acc. to outer loop
            for (b = 0; b <= a; b++) {
                // printing stars
                System.out.print("* ");
            }
 
            // end-line
            System.out.println();
        }
    }
 
    // Driver Function
    public static void main(String args[])
    {
        int k = 5;
        PyramidStar(k);
    }
}


Output

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

By Recursion

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG
{   
      // similar to inner for loop
      public static void printRow(int n)
    {
         if(n == 0)
        {
            return;
        }
          System.out.print("* ");
          printRow(n - 1); // recursive call for printing desired numbers of stars in a single row
    }
   
      // similar to outer for loop
      public static void changeRow(int n) // for number of rows.....it prints stars for each row
    {   
          if(n == 0)
        {
             return;
        }
 
          changeRow(n - 1); // recursive call for next row..
          printRow(n); // prints stars in a single row..
          System.out.print("\n"); // for changing the row....new line
    }
   
    public static void main (String[] args)
    {
        GFG.changeRow(5); // no need of object as changeRow is static method...
    }
}


Output

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

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

2. After 180 degrees rotation/Mirrored pattern

Here we will print a star pyramid with a rotation of 180 degrees.

Java




import java.io.*;
 
// 180 flipped pyramid star pattern
public class GFG {
    // Function to demonstrate printing pattern
    public static void FlippedPyramidStar(int k)
    {
        int a, b;
 
        // 1st loop
        for (a = 0; a < k; a++) {
 
            // nested 2nd loop
            for (b = 2 * (k - a); b >= 0; b--) {
                // printing spaces
                System.out.print(" ");
            }
 
            // nested 3rd loop
            for (b = 0; b <= a; b++) {
                // printing stars
                System.out.print("* ");
            }
 
            // end-line
            System.out.println();
        }
    }
 
    // Driver Function
    public static void main(String args[])
    {
        int k = 5;
        FlippedPyramidStar(k);
    }
}


Output

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

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

3. Printing Triangles:

Java




import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks {
    // Function to demonstrate printing pattern
    public static void printTriangle(int n)
    {
        // outer loop to handle number of rows
        // n in this case
        for (int i = 0; i < n; i++) {
 
            // inner loop to handle number spaces
            // values changing acc. to requirement
            for (int j = n - i; j > 1; j--) {
                // printing spaces
                System.out.print(" ");
            }
 
            // inner loop to handle number of columns
            // values changing acc. to outer loop
            for (int j = 0; j <= i; j++) {
                // printing stars
                System.out.print("* ");
            }
 
            // ending line after each row
            System.out.println();
        }
    }
 
    // Driver Function
    public static void main(String args[])
    {
        int n = 5;
        printTriangle(n);
    }
}


Output

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

4-Star pattern 

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
         
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}


Output

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

5-  Another different Star  pattern

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
         int n=5;
        for (int i=n;i>=1;i--)
        {
            for (int j=i;j>=1;j--)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}


Output

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

By Recursion

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG
{   
      // similar to inner for loop
      public static void printRow(int n)
    {
         if(n == 0)
        {
            return;
        }
          System.out.print("* ");
          printRow(n - 1); // recursive call for printing desired numbers of stars in a single row
    }
   
      // similar to outer for loop
      public static void changeRow(int n) // for number of rows.....it prints stars for each row
    {   
          if(n == 0)
        {
             return;
        }
         
          printRow(n); // prints stars in a single row..
          System.out.print("\n"); // for changing the row....new line
          changeRow(n - 1); // recursive call for next row..
    }
   
    public static void main (String[] args)
    {
        GFG.changeRow(5); // no need of object as changeRow is static method...
    }
}


Output

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

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



Last Updated : 29 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads