Print the pyramid pattern with given height and minimum number of stars
Last Updated :
07 Aug, 2022
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.
- Run a loop to print spaces required.
- Print the left pyramid.
- Run a loop to print spaces required in the middle.
- Print the right pyramid.
Below is the program to print the above pattern:
C++
#include <bits/stdc++.h>
using namespace std;
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 (i = 0; i < p_height; i++) {
for (j = p_space; j > i; j--) {
cout << " " ;
}
for (k = 0; k < min_stars; k++)
cout << "*" ;
for (n = (p_height + p_height - 2);
n >= x; n--)
cout << " " ;
for (k = 0; k < min_stars; k++)
cout << "*" ;
min_stars = min_stars + 2;
x = x + 2;
cout << endl;
}
}
int main()
{
int min_stars = 1;
int p_height = 5;
pattern(min_stars, p_height);
return 0;
}
|
C
#include <stdio.h>
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 (i = 0; i < p_height; i++) {
for (j = p_space; j > i; j--) {
printf ( " " );
}
for (k = 0; k < min_stars; k++)
printf ( "*" );
for (n = (p_height + p_height - 2); n >= x; n--)
printf ( " " );
for (k = 0; k < min_stars; k++)
printf ( "*" );
min_stars = min_stars + 2;
x = x + 2;
printf ( "\n" );
}
}
int main()
{
int min_stars = 1;
int p_height = 5;
pattern(min_stars, p_height);
return 0;
}
|
Java
import java.io.*;
class GFG
{
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 (i = 0 ; i < p_height; i++)
{
for (j = p_space; j > i; j--)
{
System.out.print( " " );
}
for (k = 0 ; k < min_stars; k++)
System.out.print( "*" );
for (n = (p_height + p_height - 2 );
n >= x; n--)
System.out.print( " " );
for (k = 0 ; k < min_stars; k++)
System.out.print( "*" );
min_stars = min_stars + 2 ;
x = x + 2 ;
System.out.println();
}
}
public static void main (String[] args)
{
int min_stars = 1 ;
int p_height = 5 ;
pattern(min_stars, p_height);
}
}
|
Python3
def pattern(min_stars, p_height):
p_space = p_height - 1
x = 1
for i in range ( 0 ,p_height) :
for j in range (p_space,i , - 1 ) :
print ( " " ,end = "")
for k in range ( 0 ,min_stars) :
print ( "*" ,end = "")
for n in range ((p_height + p_height - 2 ), x - 1 , - 1 ) :
print ( " " ,end = "")
for k in range ( 0 ,min_stars) :
print ( "*" ,end = "")
min_stars = min_stars + 2
x = x + 2
print ("")
if __name__ = = '__main__' :
min_stars = 1
p_height = 5
pattern(min_stars, p_height)
|
C#
using System;
class GFG
{
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 (i = 0; i < p_height; i++)
{
for (j = p_space; j > i; j--)
{
Console.Write( " " );
}
for (k = 0; k < min_stars; k++)
Console.Write( "*" );
for (n = (p_height +
p_height - 2);
n >= x; n--)
Console.Write( " " );
for (k = 0; k < min_stars; k++)
Console.Write( "*" );
min_stars = min_stars + 2;
x = x + 2;
Console.WriteLine();
}
}
static public void Main ()
{
int min_stars = 1;
int p_height = 5;
pattern(min_stars, p_height);
}
}
|
PHP
<?php
function pattern( $min_stars ,
$p_height )
{
$p_space ;
$p_space = $p_height - 1;
$i ; $j ; $k ; $n ; $x ;
$x = 1;
for ( $i = 0; $i < $p_height ; $i ++)
{
for ( $j = $p_space ;
$j > $i ; $j --)
{
echo " " ;
}
for ( $k = 0;
$k < $min_stars ; $k ++)
echo "*" ;
for ( $n = ( $p_height +
$p_height - 2);
$n >= $x ; $n --)
echo " " ;
for ( $k = 0;
$k < $min_stars ; $k ++)
echo "*" ;
$min_stars = $min_stars + 2;
$x = $x + 2;
echo "\n" ;
}
}
$min_stars = 1;
$p_height = 5;
pattern( $min_stars , $p_height );
?>
|
Javascript
<script>
function pattern(min_stars, p_height) {
var p_space;
p_space = p_height - 1;
var i, j, k, n, x;
x = 1;
for (i = 0; i < p_height; i++) {
for (j = p_space; j > i; j--) {
document.write( " " );
}
for (k = 0; k < min_stars; k++) document.write( "*" );
for (n = p_height + p_height - 2; n >= x; n--)
document.write( " " );
for (k = 0; k < min_stars; k++) document.write( "*" );
min_stars = min_stars + 2;
x = x + 2;
document.write( "<br>" );
}
}
var min_stars = 1;
var p_height = 5;
pattern(min_stars, p_height);
</script>
|
Output:
* *
*** ***
***** *****
******* *******
******************
Time complexity: O(n2) where n is given height of the pyramid
Space complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...