Skip to content
Related Articles

Related Articles

Java Program to Print Pyramid Star Pattern
  • Last Updated : 09 Mar, 2021

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
* 
* * 
* * * 
* * * * 
* * * * * 

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
           * 
         * * 
       * * * 
     * * * * 
   * * * * * 

3. Printing Triangles:

Java




import java.io.*;
  
// Java code to demonstrate star pattern
public class GeeksForGeeks {
    // Function to demonstrate printing pattern
    public static void printTriagle(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;
        printTriagle(n);
    }
}
Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :