The Wayback Machine - https://web.archive.org/web/20240928070511/https://www.geeksforgeeks.org/program-to-print-pyramid-pattern/
Open In App

Program to print pyramid pattern

Last Updated : 22 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Write to program to print the pyramid pattern formed of stars 

Example : 

Input:  n = 6
Output:
       *
       * *
       * * *
       * * * *
       * * * * *
       * * * * * * 
       * * * * *
       * * * *
       * * *
       * * 
       *

We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use two for loops for every part of the pyramid. The two parts may be classified as upper part and lower part 

C++




// C++ program to print Pyramid pattern
#include<iostream>
using namespace std;
 
void pattern(int n)
{   
    // For printing the upper part of the pyramid
    for (int i = 1; i < n; i++){
        for (int j = 1; j < i+1; j++){
            cout <<" * ";
        }
        cout << endl ;
    }
 
    // For printing the lower part of pyramid
    for (int i = n; i > 0; i--){
        for (int j = i; j > 0; j--){
            cout <<  " * ";
        }
        cout << endl ;
    }
}
 
// Driver program
int main()
{
    pattern(6);
    return 0;
}


C




// C program to print the pyramid pattern
#include <stdio.h>
 
void pattern(int n)
{
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
 
    // For printing the lower part of pyramid
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
}
 
// Driver program
int main()
{
    pattern(6);
    return 0;
}
 
// This code is contributed by bhartik021


Java




// Java program to print Pyramid pattern
import java.io.*;
 
class GFG
{
    public static void pattern(int n)
    {
        // For printing the upper
        // part of the pyramid
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < i+1; j++)
            {
                System.out.print(" * ");
            }
            System.out.println();
        }
     
        // For printing the lower
        // part of pyramid
        for (int i = n; i > 0; i--)
        {
            for (int j = i; j > 0; j--)
            {
                System.out.print(" * ");
            }
            System.out.println();
        }
    }
     
    // Driver program
    public static void main(String args[])
    {
        pattern(6);
    }
}
     
// This code is contributed by NIkita Tiwari.


Python3




# Python program to print Pyramid pattern
 
def pattern(n):
    
    # For printing the upper part of pyramid
    for i in range (1, n+1):
        for j in range (1, i+1):
            print (" *",end=" ")
        print()
     
    # for printing the middle and lower part of pyramid
    for i in range (n, 1, -1):
        for j in range (i, 1, -1):
            print (" *",end=" ")
        print()
 
# Driver program
pattern(6)


C#




// C# program to print Pyramid pattern
using System;
 
class GFG {
    public static void pattern(int n)
    {
        // For printing the upper
        // part of the pyramid
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < i + 1; j++) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
 
        // For printing the lower
        // part of pyramid
        for (int i = n; i > 0; i--) {
            for (int j = i; j > 0; j--) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
    }
 
    // Driver program
    public static void Main()
    {
        pattern(6);
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP implementation to print
// Pyramid pattern
 
function pattern($n)
{
    // For printing the upper part
    // of the pyramid
    for ($i = 1; $i < $n; $i++)
    {
        for ($j = 1; $j < $i+1; $j++)
        {
            echo " * ";
        }
        echo "\n" ;
    }
 
    // For printing the lower part
    // of pyramid
    for ($i = $n; $i > 0; $i--)
    {
        for ($j = $i; $j > 0; $j--)
        {
            echo " * ";
        }
        echo "\n" ;
    }
}
 
// Driver code
$n=6;
pattern($n);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// JavaScript program to print Pyramid pattern
function pattern(n)
{
     
    // For printing the upper
    // part of the pyramid
    for(var i = 1; i < n; i++)
    {
        for(var j = 1; j < i + 1; j++)
        {
            document.write(" * ");
        }
        document.write("<br>");
    }
     
    // For printing the lower part of pyramid
    for(var i = n; i > 0; i--)
    {
        for(var j = i; j > 0; j--)
        {
            document.write(" * ");
        }
        document.write("<br>");
    }
}
 
// Driver code
pattern(6);
 
// This code is contributed by rdtank
 
</script>


Output : 

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

Time complexity: O(n2)

Auxiliary Space: O(1)

 



Previous Article
Next Article

Similar Reads

Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Program to Print Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *** ***** **************** Approach: The problem ca
4 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Program to print a inverse pyramid character pattern
Given a positive integer n, print the inverse pyramid pattern upto n rows as shown in the examples.Examples : Input : 4 Output : A B C D D C B A A B C C B A A B B A A A Input : 6 Output : A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A Below is the implementation for the pattern: C/C++ Code // C++ code to print i
6 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
15+ min read
Print the following pyramid pattern
Given a positive integer n. The problem is to print the pyramid pattern as described in the examples below. Examples: Input : n = 4 Output : 1 3*2 4*5*6 10*9*8*7 Input : n = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Source: Amdocs Interview Experience | Set 1 Approach: For odd number row, values are being displayed in increasing order and for
6 min read
Program to Print Butterfly Pattern (Star Pattern)
Given an integer N, print N rows of Butterfly pattern. Examples: Input: 3Output: * *** ********* *** * Input: 5Output: * *** ***** ******* ***************** ******* ***** *** * Approach: The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, the first inner loop will print the stars, the secon
6 min read
Program to print Even Odd Number Pyramid
Given the total number of rows as n, the task is to print the given pattern. * 1* *2* 1*3* *2*4* 1*3*5* *2*4*6* 1*3*5*7* *2*4*6*8* 1*3*5*7*9* . . Examples: Input: n = 5 Output: * 1* *2* 1*3* *2*4* Input: n = 10 Output: * 1* *2* 1*3* *2*4* 1*3*5* *2*4*6* 1*3*5*7* *2*4*6*8* 1*3*5*7*9* Below is the solution to the above problem: C/C++ Code // CPP prog
4 min read
C++ Program To Print Pyramid Patterns
In this article, we will discuss the following top 16 pattern programs in C++ using star ( * ), numbers or other characters. C++ Programs to Print Patterns and PyramidsTable of Content Simple Pyramid Pattern in C++Flipped Simple Pyramid Pattern in C++Inverted Pyramid Pattern in C++Flipped Inverted Pyramid Pattern in C++Triangle Pattern in C++Invert
15+ min read
Hollow Half Pyramid Pattern Using Numbers
A hollow half-pyramid pattern using numbers is a type of pattern that seems like a pyramid shape mostly it is considered a star pattern but here we will be creating using numbers.  Program to print the following pattern of a half pyramid for N. Example:  Input: N = 5 1 1 2 1 3 1 4 1 2 3 4 5 Below is the implementation of the above approach: [GFGTAB
8 min read
Program to print interesting pattern
Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4**
10 min read
Program to print number pattern
We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Output : 1 2 1 2 3 2 1 2 3 2 1 2 1 Below is the impleme
7 min read
Program to print number with star pattern
We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C/C++ Code #include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n
6 min read
Program to print double headed arrow pattern
Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as- Examples: Inpu
9 min read
Program to print V and inverted-V pattern
Inverted V pattern: Given the value of n, print the inverted V pattern.Examples : Input : n = 5 Output : E D D C C B B A A Input : n = 7 Output : G F F E E D D C C B B A A Below is the program to print the above pattern C/C++ Code // C++ Implementation to print the pattern #include <bits/stdc++.h> using namespace std; // Function definition v
8 min read
Program to print the arrow pattern
Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C/C++ Code // C++ program to print the // arrow pattern #include <bits/stdc++.h> using namespace
10 min read
Program to print the pattern "GFG"
In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern "GFG" using stars and white-spaces. Examples: INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OUTPUT: ******* ******* ******* * * * * * * * * * *
8 min read
Program to print mirror image of Swastika Pattern
Given the number of rows and columns, print the mirror image of swastika pattern.Note : The number of rows and columns should be same and an odd number. This will generate a perfect mirror image of swastika pattern. Examples : Input : row = 5, col = 5 Output : * * * * * * * * * * * * * * * * * Input : row = 9, col = 9 Output : * * * * * * * * * * *
12 min read
Program to print circle pattern
Given a positive integer n i.e, the radius of the circle, print a circle using stars. Examples : Input : n = 3 Output : *** * * * * * * * * * * *** Input : n = 6 Output : ***** ** ** ** ** * * * * * * * * * * * * * * ** ** ** ** ***** Below is the implementation: C/C++ Code // C++ implementation to print circle pattern #include <bits/stdc++.h
5 min read
Program to print Crown Pattern
Given the value of length, print the crown pattern using '*' and '#'. Note : In order to print a perfect crown, take the value of length as an odd number greater than 15. Examples : Input: length = 19 Output: * * * # # # ## ### ## ### ##### ### #### ####### #### ################### ################### ################### ******************* Input:
7 min read
Program to print pattern
Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C/C++ Code // C++ program to print given pattern #include <bits/stdc++.h> using namespace std
5 min read
Program to print numeric pattern | Set - 2
Given a number as 'num', and a number of lines as 'num_of_lines' where 'num' implies the starting number from which the pattern has to be started and 'num_of_lines' implies the number of lines that have to be printed. Now, according to the above information, print a pattern as given below. Examples: Input: num = 7, num_of_lines = 4 Output: 7 14 15
5 min read
Program to print Sine-Wave Pattern
Give the Height and Width of a Wave to print the Sine wave pattern Examples: Input : wave_height=5 wave_length=10 Output : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Recommended: Please try you
15 min read
Program to Print Mirror Image of Sine-Wave Pattern
A sine wave is a mathematical curve that oscillates between a maximum and minimum value, smoothly transitioning between positive and negative values. It is commonly used to represent periodic phenomena such as sound, light, and electrical signals. Examples: Give the Height and Width of a Wave to print the pattern. Input : wave_height=5 wave_length=
5 min read
Program to Print the Trapezium Pattern
Given 'num' which indicates number of lines.The task is to print a trapezium pattern in num lines.Examples: Input : 4 Output : 1*2*3*4*17*18*19*20 5*6*7*14*15*16 8*9*12*13 10*11 Input : 2 Output : 1*2*5*6 3*4 Algorithm : step 1. To read num which indicates the number of lines. step 2.We are diving the pattern into 2 halves that is LHS part and the
6 min read
Program to print the pattern with two hollow Triangles
Given a number n, the task is to write a program to draw the following pattern using '$'. $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Approach: The above pattern is printed by breaking it into smaller patterns: The above triangle $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $This is
15+ min read
Program to print the Ladder Pattern
Given an integer N, the task is to print the ladder with N steps using '*'. The ladder will be with the gap of 3 spaces between two side rails. Input: N = 3 Output: * * * * ***** * * * * ***** * * * * ***** * * * * Input: N = 4 Output: * * * * ***** * * * * ***** * * * * ***** * * * * ***** * * * * Approach: Dividing the pattern into two sub-patter
4 min read
Practice Tags :