Open In App

Programs for printing pyramid patterns in Java

Last Updated : 03 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

This article is aimed at giving a Java implementation for pattern printing. 

Simple pyramid pattern

Java
import java.io.*;

// Java code to demonstrate star patterns
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printStars(int n)
    {
        int i, j;

        // outer loop to handle number of rows
        //  n in this case
        for(i=0; i<n; i++)
        {

            //  inner loop to handle number of columns
            //  values changing acc. to outer loop    
            for(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;
        printStars(n);
    }
}

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Time Complexity: O(n^2)
Space Complexity: O(1)

Method: Using while loop 

Java
// java program to print simple pyramid pattern using while
// loop

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int r = 1, c = 0, n = 5;
        // the while loop check the conditions until the
        // condition is false. if it is true then enter in
        // to loop and execute the statements
        while (r <= n) {
            while (c <= r - 1) {
                // printing the required pattern
                System.out.print("* ");
                c++;
            }
            r++;
            c = 0;
            // new line after each row
            System.out.println();
        }
    }
}

// this code is contributed by gangarajula laxmi

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Time Complexity: O(N^2),The outer while loop iterates N times, while the inner while loop iterates N-1 times. Hence, the total time complexity of the program will be O(N2).
Space Complexity: O(1),No extra space is required.
 

Method#2: Using Recursion

Java
// Java code to demonstrate star pattern
// using Recursion
import java.io.*;

class GFG {

    // function to print a row
    static void printRow(int num)
    {
        // base case
        if (num == 0)
            return;
        System.out.print("* ");

        // recursively calling printRow()
        printRow(num - 1);
    }

    // function to print the pattern
    static void pattern(int n, int i)
    {
        // base case
        if (n == 0)
            return;
        printRow(i);
        System.out.println();

        // recursively calling pattern()
        pattern(n - 1, i + 1);
    }

    // Driver code
    public static void main(String[] args)
    {

        int n = 5;
        pattern(n, 1);
    }
}
// this code is contributed by Shivesh Kumar Dwivedi

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Time Complexity: O(N^2)

Auxiliary Space: O(N^2)

After 180 degree rotation

Java
import java.io.*;

// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printStars(int n)
    {
        int i, j;

        // outer loop to handle number of rows
        //  n in this case
        for(i=0; i<n; i++)
        {

            // inner loop to handle number spaces
            // values changing acc. to requirement
            for(j=2*(n-i); j>=0; j--)
            {
                // printing spaces
                System.out.print(" ");
            }
           
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for(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;
        printStars(n);
    }
}

Output
           * 
         * * 
       * * * 
     * * * * 
   * * * * * 

The time complexity of the program is O(n^2) because there are two nested loops, each running n times. 

The space complexity is O(1).

Method#3: Using Recursion

Java
// Java code to demonstrate star pattern
//using Recursion
import java.util.*;
 
public class GeeksForGeeks
{
 
    // function to print spaces
    static void printSpace(int space)
    {
        // base case
        if (space == 0)
        {
            return;
        }
        System.out.print(" " + " ");
 
        // recursively calling printSpace()
        printSpace(space - 1);
    }
 
    // function to print stars
    static void printStars(int star)
    {
        // base case
        if (star == 0)
        {
            return;
        }
        System.out.print("* ");
 
        // recursively calling printStars()
        printStars(star - 1);
    }
 
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
        {
            return;
        }
        printSpace(n - 1);
        printStars(num - n + 1);
        System.out.println();
 
        // recursively calling pattern()
        pattern(n - 1, num);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        pattern(n, n);
    }
}
//this code is contributed by Shivesh Kumar Dwivedi

Output
        * 
      * * 
    * * * 
  * * * * 
* * * * * 

Printing Triangle

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

Method#4: Using Recursion

Java
// Java code to demonstrate star pattern
// using recursion
import java.util.*;

public class GeeksForGeeks {

    // function to print spaces
    static void printSpace(int space)
    {
        // base case
        if (space == 0)
            return;
        System.out.print(" ");

        // recursively calling printSpace()
        printSpace(space - 1);
    }

    // function to print asterisks
    static void printStar(int asterisk)
    {
        // base case
        if (asterisk == 0)
            return;
        System.out.print("* ");

        // recursively calling printStar()
        printStar(asterisk - 1);
    }

    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
            return;
        printSpace(n - 1);
        printStar(num - n + 1);
        System.out.println("");

        // recursively calling pattern()
        pattern(n - 1, num);
    }

    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n, n);
    }
}
// this code is contributed by Shivesh Kumar Dwivedi

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Print Reverse Of Pyramid

Java
//MainFunction
public class ReversePyramid
{
    public static void main(String[] args) 
    {
        int rows = 6; // Number of Rows we want to print
         
        
         
        //Printing the pattern
        for (int i = 1; i <= rows; i++) 
        {
          for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            } 
          for (int j = i; j <= rows; j++) 
            { 
                System.out.print(j+" "); 
            }   
            System.out.println(); 
        } 
       

       
        }
    
    }

Output
1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 

Pattern of Number with Mirror Image

Java
//MainFunction
public class ReversePattern
{
    public static void main(String[] args) 
    {
        int rows = 7; // Number of Rows we want to print
         
        
         
        //Printing the pattern
        for (int i = 1; i <= rows; i++) 
        {
          for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            } 
          for (int j = i; j <= rows; j++) 
            { 
                System.out.print(j+" "); 
            }   
            System.out.println(); 
        } 
       

       //Printing the reverse pattern
        for (int i = rows-1; i >= 1; i--) 
        {
          for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }  
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }   
            System.out.println();
        }
    
    }
}

Output
1 2 3 4 5 6 7 
 2 3 4 5 6 7 
  3 4 5 6 7 
   4 5 6 7 
    5 6 7 
     6 7 
      7 
     6 7 
    5 6 7 
   4 5 6 7 
  3 4 5 6 7 
 2 3 4 5 6 7 
1 2 3 4 5 6 7 

Number Pattern

Java
import java.io.*;

// Java code to demonstrate number pattern
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printNums(int n)
    {
        int i, j,num;

        // outer loop to handle number of rows
        //  n in this case
        for(i=0; i<n; i++)
        {
            // initialising starting number
            num=1;

            //  inner loop to handle number of columns
            //  values changing acc. to outer loop
            for(j=0; j<=i; j++)
            {
                // printing num with a space 
                System.out.print(num+ " ");

                //incrementing value of num
                num++;
            }

            // ending line after each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 5;
        printNums(n);
    }
}

Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Method5: Using while loop

Java
// java program to print number pattern using while
// loop

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int r = 1, c = 1, n = 5;
        // the while loop check the conditions until the
        // condition is false. if it is true then enter in
        // to loop and execute the statements
        while (r <= n) {
            while (c <= r ) {
                // printing the required pattern
                System.out.print(c+" ");
                c++;
            }
            r++;
            c = 1;
            // new line after each row
            System.out.println();
        }
    }
}

Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Numbers without re assigning

Java
import java.io.*;

// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printNums(int n)
    {
        // initialising starting number
        int i, j, num=1;
        
        // outer loop to handle number of rows
        // n in this case
        for(i=0; i<n; i++)
        {

            // without re assigning num
            // num = 1;
            for(j=0; j<=i; j++)
            {
                // printing num with a space
                System.out.print(num+ " ");
                
                // incrementing num at each column
                num = num + 1;
            }

            // ending line after each row
            System.out.println();
        }
    }
    
    // Driver Function
    public static void main(String args[])
    {
        int n = 5;
        printNums(n);
    }
}

Output
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

Printing Christmas Tree Using Pyramid

Java
class  PrintChristmasTree{

    //Value 5 is permanently provided to height variable
  public static final int height = 5;
  
   //Main Function
  public static void main(String[] args) {
    
    //Assigning Width 
    int width = 5;

    //Assigning Space
    int space = width*5;

    int x = 1;

    //Code to Print Upper Part of the Tree i.e. Pyramids.
    for(int a = 1;a <= height ;a++){

      for(int i = x;i <= width;i++){

        for(int j = space;j >= i;j--){

          System.out.print(" ");
        }

        for(int k = 1;k <= i;k++){

          System.out.print("* ");
        }

        System.out.println();
      }

      x = x+2;
      width = width+2;
    }


    //Printing  Branch of Christmas Tree
    for(int i = 1;i <= 4;i++){

      for(int j = space-3;j >= 1;j--){
        
        System.out.print(" ");
      }

      for(int k= 1;k <= 4;k++){
        System.out.print("* ");
      }

      System.out.println();
    }
  }
}

Output
                         * 
                        * * 
                       * * * 
                      * * * * 
                     * * * * * 
                       * * * 
                      * * * * 
                     * * * * * 
                    * * * * * * 
                   * * * * * * * 
                     * * * * * 
                    * * * * * * 
                   * * * * * * * 
                  * * * * * * * * 
                 * * * * * * * * * 
                   * * * * * * * 
                  * * * * * * * * 
                 * * * * * * * * * 
                * * * * * * * * * * 
               * * * * * * * * * * * 
                 * * * * * * * * * 
                * * * * * * * * * * 
               * * * * * * * * * * * 
              * * * * * * * * * * * * 
             * * * * * * * * * * * * * 
                      * * * * 
                      * * * * 
                      * * * * 
                      * * * * 

Time complexity: O(h*w*w) for given height h and width w

Auxiliary Space: O(1)


 


Pyramid Pattern
Visit Course explore course icon
Article Tags :
Practice Tags :

Similar Reads