C++ Program To Print Pyramid Patterns

Last Updated : 26 Aug, 2026

Pattern programs are a great way to practice loops and strengthen your C++ programming and problem-solving skills. The C++ Course provides hands-on examples and exercises to help you learn and implement different patterns.

  • Practice common star, number, and character patterns.
  • Improve your understanding of nested loops and pattern logic.
patterns
C++ Programs to Print Patterns and Pyramids

Simple Pyramid Pattern

Method 1

Printing simple pyramid pattern using for loop

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while Loop

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << " ";
            j++;
        }

        // Print stars
        j = 1;
        while (j <= i) {
            cout << "* ";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

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

Method 3

Printing the above pattern using recursion.

C++
#include <iostream>
using namespace std;

void printStars(int count)
{
    if (count == 0)
        return;

    cout << "* ";
    printStars(count - 1);
}

void printPyramid(int row, int n)
{
    if (row > n)
        return;

    // Print leading spaces
    for (int i = 1; i <= n - row; i++) {
        cout << " ";
    }

    // Print stars
    printStars(row);

    cout << endl;

    // Print the next row
    printPyramid(row + 1, n);
}

int main()
{
    int n = 5;

    printPyramid(1, n);

    return 0;
}

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

Flipped Simple Pyramid Pattern

Method 1

Printing the 180° rotated simple pyramid pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    for (int i = n; i >= 1; i--) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = n;

    while (i >= 1) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << " ";
            j++;
        }

        // Print stars
        j = 1;
        while (j <= i) {
            cout << "* ";
            j++;
        }

        cout << endl;
        i--;
    }

    return 0;
}

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

Inverted Pyramid Pattern

Method 1

Printing the pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    for (int i = n; i >= 1; i--) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = n;

    while (i >= 1) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << " ";
            j++;
        }

        // Print stars
        j = 1;
        while (j <= i) {
            cout << "* ";
            j++;
        }

        cout << endl;
        i--;
    }

    return 0;
}

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

Method 3

Printing the above pattern using recursion.

C++
#include <iostream>
using namespace std;

void printRow(int stars, int spaces)
{
    if (spaces > 0) {
        cout << " ";
        printRow(stars, spaces - 1);
    }
    else if (stars > 0) {
        cout << "* ";
        printRow(stars - 1, 0);
    }
}

void printPattern(int row, int n)
{
    if (row > n)
        return;

    printRow(n - row + 1, row - 1);
    cout << endl;

    printPattern(row + 1, n);
}

int main()
{
    int n = 5;

    printPattern(1, n);

    return 0;
}

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

Flipped Inverted Pyramid Pattern

Method 1

Printing this pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << " ";
            j++;
        }

        // Print stars
        j = 1;
        while (j <= 2 * i - 1) {
            cout << "*";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

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

Triangle Pattern

Method 1

Printing the given pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = 1;

    while (i <= n) {
        int j = 1;

        while (j <= i) {
            cout << "* ";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

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

Inverted Triangle Pattern

Method 1

Printing the inverted triangle pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = n;

    while (i >= 1) {
        int j = 1;

        while (j <= i) {
            cout << "* ";
            j++;
        }

        cout << endl;
        i--;
    }

    return 0;
}

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

Half Diamond Pattern

C++
#include <iostream>
using namespace std;

// Function to print rotated triangle pattern of 2*n-1 rows
// and n columns
void printRotatedTriangle(int n)
{
    // First we print upper n rows out of the total 2*n-1
    // rows
    for (int i = 1; i <= n; i++) {

        // j iterates till i to print i number of stars
        // (*) in the (i)th row separated with one space
        for (int j = 1; j <= i; j++) {
              //Printing stars separated by one space
            cout << "*" << " ";
        }
        // line ends after printing i stars in (i)th row
        cout << endl;
    }
    // Now, we print other (n-1) rows of the triangle
    for (int i = 1; i <= n - 1; i++) {

        // j iterates till n-i to print n-i number of
        // stars (*) in the (i)th row separated with one
        // space
        for (int j = 1; j <= n - i; j++) {
              //Printing stars separated by one space
            cout << "*" << " ";
        }
        // line ends after printing n-i stars in (i)th row
        cout << endl;
    }
}

//Driver Code
int main()
{

    int n = 5;
    //Function Call
    printRotatedTriangle(n);
    return 0;
}

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

Flipped Half Diamond Pattern

C++
#include <iostream>
using namespace std;

// Function to print the flipped half-diamond pattern
// with n columns and 2*n-1 rows
void printFlippedHalfDiamond(int n)
{
    // Print the upper half
    for (int i = 1; i <= n; i++) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int k = 1; k <= i; k++) {
            cout << "*";
        }

        cout << endl;
    }

    // Print the lower half
    for (int i = 1; i <= n - 1; i++) {

        // Print leading spaces
        for (int j = 1; j <= i; j++) {
            cout << " ";
        }

        // Print stars
        for (int k = 1; k <= n - i; k++) {
            cout << "*";
        }

        cout << endl;
    }
}

int main()
{
    int n = 5;

    printFlippedHalfDiamond(n);

    return 0;
}

Output

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

Diamond Shaped Pattern

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    // Upper half
    for (int i = 1; i <= n; i++) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }

        cout << endl;
    }

    // Lower half
    for (int i = n - 1; i >= 1; i--) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }

        cout << endl;
    }

    return 0;
}

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

Hourglass Pattern

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

    // Upper half
    for (int i = n; i >= 1; i--) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }

        cout << endl;
    }

    // Lower half
    for (int i = 2; i <= n; i++) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }

        cout << endl;
    }

    return 0;
}

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

Number Pyramid Pattern

Method 1

Printing the number pyramid pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print numbers
        for (int j = 1; j <= i; j++) {
            cout << j << " ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << "  ";
            j++;
        }

        // Print numbers
        j = 1;
        while (j <= i) {
            cout << j << " ";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

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

Numbers Pyramid Pattern without Reassigning

Method 1

Printing the number pyramid pattern in C++ without reassigning using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int num = 1;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print numbers without reassigning
        for (int j = 1; j <= i; j++) {
            cout << num++ << " ";
        }

        cout << endl;
    }

    return 0;
}

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

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int num = 1;
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << "  ";
            j++;
        }

        // Print continuous numbers
        j = 1;
        while (j <= i) {
            cout << num++ << " ";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

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

Continuous Number Pyramid after 180° Rotation

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int num = n * (n + 1) / 2;

    for (int i = n; i >= 1; i--) {

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print continuous numbers
        for (int j = 1; j <= i; j++) {
            cout << num - i + j << " ";
        }

        num -= i;
        cout << endl;
    }

    return 0;
}

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

Palindrome Triangle Pattern

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print increasing numbers
        for (int j = 1; j <= i; j++) {
            cout << j << " ";
        }

        // Print decreasing numbers
        for (int j = i - 1; j >= 1; j--) {
            cout << j << " ";
        }

        cout << endl;
    }

    return 0;
}

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

Alphabet Pyramid Pattern

Method 1

Printing the given pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print alphabets
        for (int j = 1; j <= i; j++) {
            cout << char('A' + j - 1) << " ";
        }

        cout << endl;
    }

    return 0;
}

Output
        A 
      A B 
    A B C 
  A B C D 
A B C D E 

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << "  ";
            j++;
        }

        // Print alphabets
        j = 1;
        while (j <= i) {
            cout << char('A' + j - 1) << " ";
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

Output
A 
B B 
C C C 
D D D D 
E E E E E 

Continuous Alphabet Pyramid Pattern

Method 1

Printing the above pattern using for loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    char ch = 'A';

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

        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            cout << "  ";
        }

        // Print continuous alphabets
        for (int j = 1; j <= i; j++) {
            cout << ch << " ";
            ch++;
        }

        cout << endl;
    }

    return 0;
}

Output
        A 
      B C 
    D E F 
  G H I J 
K L M N O 

Method 2

Printing the above pattern using while loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    char ch = 'A';
    int i = 1;

    while (i <= n) {
        int j = 1;

        // Print leading spaces
        while (j <= n - i) {
            cout << "  ";
            j++;
        }

        // Print continuous alphabets
        j = 1;
        while (j <= i) {
            cout << ch << " ";
            ch++;
            j++;
        }

        cout << endl;
        i++;
    }

    return 0;
}

Output
        A 
      B C 
    D E F 
  G H I J 
K L M N O 
Comment