Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Print the pyramid pattern with given height and minimum number of stars

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given the minimum number of stars and the height of the pyramid, print the pattern.
Examples: 
 

Input: min_stars = 1
        p_height = 5
Output:

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

Input: min_stars = 3
        p_height = 7
Output:

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

 

The approach is to run a loop up to the height of the pyramid.
 

  1. Run a loop to print spaces required.
  2. Print the left pyramid.
  3. Run a loop to print spaces required in the middle.
  4. Print the right pyramid.

Below is the program to print the above pattern: 
 

C++




// C++ implementation to print the pattern
#include <bits/stdc++.h>
using namespace std;
 
// Function definition
void pattern(int min_stars, int p_height)
{
    int p_space;
    p_space = p_height - 1;
    int i, j, k, n, x;
    x = 1;
 
    // for loop for height of pyramid
    for (i = 0; i < p_height; i++) {
 
        // for loop for spaces
        for (j = p_space; j > i; j--) {
            cout << " ";
        }
 
        // for loop for printing
        // left pyramid
        for (k = 0; k < min_stars; k++)
            cout << "*";
 
        // for loop for spaces in middle
        for (n = (p_height + p_height - 2);
             n >= x; n--)
            cout << " ";
 
        // for loop for printing
        // right pyramid
        for (k = 0; k < min_stars; k++)
            cout << "*";
 
        min_stars = min_stars + 2;
        x = x + 2;
        cout << endl;
    }
}
 
// Driver code
int main()
{
    /* change value to set minimum,
    number of stars in pyramid */
    int min_stars = 1;
 
    /* change value to increase or
    decrease size of pyramid */
    int p_height = 5;
 
    // function calling
    pattern(min_stars, p_height);
    return 0;
}

C




// C implementation to print the pattern
#include <stdio.h>
 
// Function definition
void pattern(int min_stars, int p_height)
{
  int p_space;
  p_space = p_height - 1;
  int i, j, k, n, x;
  x = 1;
 
  // for loop for height of pyramid
  for (i = 0; i < p_height; i++) {
 
    // for loop for spaces
    for (j = p_space; j > i; j--) {
      printf(" ");
    }
 
    // for loop for printing
    // left pyramid
    for (k = 0; k < min_stars; k++)
      printf("*");
 
    // for loop for spaces in middle
    for (n = (p_height + p_height - 2); n >= x; n--)
      printf(" ");
 
    // for loop for printing
    // right pyramid
    for (k = 0; k < min_stars; k++)
      printf("*");
 
    min_stars = min_stars + 2;
    x = x + 2;
    printf("\n");
  }
}
 
// Driver code
int main()
{
  /* change value to set minimum,
    number of stars in pyramid */
  int min_stars = 1;
 
  /* change value to increase or
    decrease size of pyramid */
  int p_height = 5;
 
  // function calling
  pattern(min_stars, p_height);
  return 0;
}
 
// This code is contributed by bhartik021.

Java




// Java implementation
// to print the pattern
import java.io.*;
 
class GFG
{
     
// Function definition
static void pattern(int min_stars,
                    int p_height)
{
    int p_space;
    p_space = p_height - 1;
    int i, j, k, n, x;
    x = 1;
 
    // for loop for
    // height of pyramid
    for (i = 0; i < p_height; i++)
    {
 
        // for loop for spaces
        for (j = p_space; j > i; j--)
        {
            System.out.print(" ");
        }
 
        // for loop for printing
        // left pyramid
        for (k = 0; k < min_stars; k++)
            System.out.print("*");
 
        // for loop for
        // spaces in middle
        for (n = (p_height + p_height - 2);
             n >= x; n--)
                System.out.print(" ");
 
        // for loop for printing
        // right pyramid
        for (k = 0; k < min_stars; k++)
                System.out.print("*");
 
        min_stars = min_stars + 2;
        x = x + 2;
            System.out.println();
    }
}
 
// Driver code
public static void main (String[] args)
{
 
/* change value to set minimum
number of stars in pyramid */
int min_stars = 1;
 
/* change value to increase or
decrease size of pyramid */
int p_height = 5;
 
// function calling
pattern(min_stars, p_height);
}
}
 
// This code is contributed
// by ajit

Python3




# Python3 implementation to print the pattern
 
# Function definition
def pattern(min_stars, p_height):
 
    p_space = p_height - 1
    x = 1
 
    # for loop for height of pyramid
    for i in range(0 ,p_height) :
 
        # for loop for spaces
        for j in range(p_space,i ,-1) :
            print(" " ,end="")
         
 
        # for loop for printing
        # left pyramid
        for k in range(0 ,min_stars) :
            print("*" ,end="")
 
        # for loop for spaces in middle
        for n in range ((p_height + p_height - 2), x-1,-1) :
            print(" " ,end="")
 
        # for loop for printing
        # right pyramid
        for k in range(0 ,min_stars) :
            print("*" ,end="")
 
        min_stars = min_stars + 2
        x = x + 2
        print("")
 
# Driver code
# change value to set minimum
# number of stars in pyramid
if __name__=='__main__':
    min_stars = 1
 
# change value to increase or
# decrease size of pyramid
    p_height = 5
 
# function calling
    pattern(min_stars, p_height)
 
# this code is contributed by Smitha Dinesh Semwal

C#




// C# implementation
// to print the pattern
using System;
 
class GFG
{
     
// Function definition
static void pattern(int min_stars,
                    int p_height)
{
    int p_space;
    p_space = p_height - 1;
    int i, j, k, n, x;
    x = 1;
 
    // for loop for
    // height of pyramid
    for (i = 0; i < p_height; i++)
    {
 
        // for loop for spaces
        for (j = p_space; j > i; j--)
        {
            Console.Write(" ");
        }
 
        // for loop for printing
        // left pyramid
        for (k = 0; k < min_stars; k++)
            Console.Write("*");
 
        // for loop for
        // spaces in middle
        for (n = (p_height +
                  p_height - 2);
             n >= x; n--)
                Console.Write(" ");
 
        // for loop for printing
        // right pyramid
        for (k = 0; k < min_stars; k++)
                Console.Write("*");
 
        min_stars = min_stars + 2;
        x = x + 2;
            Console.WriteLine();
    }
}
 
// Driver code
static public void Main ()
{
         
    /* change value to set
    minimum number of stars
    in pyramid */
    int min_stars = 1;
 
    /* change value to increase
    or decrease size of pyramid */
    int p_height = 5;
 
    // function calling
    pattern(min_stars, p_height);
}
}
 
// This code is contributed
// by ajit

PHP




<?php
// PHP implementation
// to print the pattern
 
// Function definition
function pattern($min_stars,
                 $p_height)
{
    $p_space;
    $p_space = $p_height - 1;
    $i; $j; $k; $n; $x;
    $x = 1;
 
    // for loop for height
    // of pyramid
    for ($i = 0; $i < $p_height; $i++)
    {
 
        // for loop for spaces
        for ($j = $p_space;
             $j > $i; $j--)
        {
            echo " ";
        }
 
        // for loop for printing
        // left pyramid
        for ($k = 0;
             $k < $min_stars; $k++)
            echo "*";
 
        // for loop for
        // spaces in middle
        for ($n = ($p_height +
                   $p_height - 2);
            $n >= $x; $n--)
            echo " ";
 
        // for loop for printing
        // right pyramid
        for ($k = 0;
             $k < $min_stars; $k++)
            echo "*";
 
        $min_stars = $min_stars + 2;
        $x = $x + 2;
        echo "\n";
    }
}
 
// Driver code
 
/* change value to set minimum
number of stars in pyramid */
$min_stars = 1;
 
/* change value to increase or
decrease size of pyramid */
$p_height = 5;
 
// function calling
pattern($min_stars, $p_height);
 
// This code is contributed by ajit
?>

Javascript




<script>
      // JavaScript implementation to print the pattern
 
      // Function definition
      function pattern(min_stars, p_height) {
        var p_space;
        p_space = p_height - 1;
        var i, j, k, n, x;
        x = 1;
 
        // for loop for height of pyramid
        for (i = 0; i < p_height; i++) {
          // for loop for spaces
          for (j = p_space; j > i; j--) {
            document.write("  ");
          }
 
          // for loop for printing
          // left pyramid
          for (k = 0; k < min_stars; k++) document.write("*");
 
          // for loop for spaces in middle
          for (n = p_height + p_height - 2; n >= x; n--)
            document.write("  ");
 
          // for loop for printing
          // right pyramid
          for (k = 0; k < min_stars; k++) document.write("*");
 
          min_stars = min_stars + 2;
          x = x + 2;
          document.write("<br>");
        }
      }
 
      // Driver code
      /* change value to set minimum,
    number of stars in pyramid */
      var min_stars = 1;
 
      /* change value to increase or
    decrease size of pyramid */
      var p_height = 5;
 
      // function calling
      pattern(min_stars, p_height);
    </script>

Output: 

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

 

Time complexity: O(n2) where n is given height of the pyramid

Space complexity: O(1)


My Personal Notes arrow_drop_up
Last Updated : 07 Aug, 2022
Like Article
Save Article
Similar Reads