Sunday, March 19, 2023
HomeSoftware developmentC++ Program To Print Pyramid Patterns

C++ Program To Print Pyramid Patterns


On this article, we’ll talk about the next prime sample packages in C++ star *, numbers, or different characters.

pyramid and patterns in c++

Pyramid Patterns in C++

  1. Easy Pyramid Sample
  2. Easy Pyramid Sample after 180° Rotation
  3. Inverted Pyramid Sample
  4. Inverted Pyramid Sample after 180° Rotation
  5. Triangle Sample
  6. Inverted Triangle Sample
  7. Quantity Pyramid Sample
  8. Numbers Pyramid Sample with out Reassigning
  9. Steady Quantity Pyramid Sample after 180° Rotation
  10. Palindrome Triangle Sample
  11. Alphabet Pyramid Sample
  12. Steady Alphabet Pyramid Sample

C++ Packages to Print Patterns and Pyramids

1. Easy Pyramid Sample in C++

Methodology 1: Printing easy pyramid sample utilizing for loop

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    

    

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

  

        

        

        for (int j = 0; j <= i; j++) {

  

            

            cout << "* ";

        }

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas Loop

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    

    

    int i = 0, j = 0;

    whereas (i < n) {

  

        

        

        whereas (j <= i) {

  

            

            cout << "* ";

            j++;

        }

        j = 0;

               

               

        i++;

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Methodology 3: Printing the above sample utilizing recursion.

C++

#embrace <iostream>

utilizing namespace std;

  

void print(int n)

{

    if(n == 0)

    {

        return;

    }

      cout << "* "

      print(n - 1);

}

  

void sample(int n)

{

    

    if (n == 0) 

    {

        return;

    }

    else 

    {

        

        sample(n - 1);

  

        

          print(n);

        

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    sample(n);

}

Output

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

2. Easy Pyramid Sample in C++ after 180° Rotation

Methodology 1: Printing the 180° rotated easy pyramid sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

int primary()

{

    int n = 5;

  

    

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

        for (int j = 1; j <= n; j++)

        {

            if (j >= i) {

                cout << "* ";

            }

            else {

                cout << "  ";

            }

        }

        cout << endl;

    }

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart2(int n)

{

    int i = 0, j = 0, ok = 0;

    whereas (i < n) {

  

        

        whereas (ok < (n - i - 1)) {

            cout << "  ";

            ok++;

        }

  

        

        

        ok = 0;

        whereas (j <= i) {

            cout << "* ";

            j++;

        }

  

        

        j = 0;

        i++;

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart2(n);

    return 0;

}

Output

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

3. Inverted Pyramid Sample in C++

Methodology 1: Printing the sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    

    

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

  

        

        

        for (int j = 0; j < i; j++) {

  

            

            cout << "* ";

        }

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Methodology 2: Printing the sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    

    

    int i = n, j = 0;

    whereas (i > 0) {

  

        

        

        whereas (j < i) {

  

            

            cout << "* ";

            j++;

        }

        j = 0;

               

               

        i--;

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Methodology 3: Printing the above sample utilizing recursion.

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

void print(int n) 

{

      if(n == 0)

    {

        return;

    }

      cout << "* ";

      print(n - 1);

}

  

void sample(int n)

{

    if (n == 0) {

        return;

    }

       print(n);

    cout << endl;

    sample(n - 1);

}

  

int primary()

{

    int n = 5;

    sample(n);

    return 0;

}

Output

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

4. Inverted Pyramid Sample in C++ after 180° Rotation 

Methodology 1: Printing this sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart2(int n)

{

    

    int ok = 2 * n - 2;

  

    

    

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

  

        

        

        for (int j = 0; j < n - i; j++)

            cout << "  ";

  

        

        ok = ok - 2;

  

        

        

        for (int j = 0; j < i; j++) {

            

            cout << "* ";

        }

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart2(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart2(int n)

{

    int i = n, j = 0, ok = 0;

    whereas (i > 0) {

  

        

        whereas (ok < (n - i)) {

            cout << "  ";

            ok++;

        }

  

        

        

        ok = 0;

        whereas (j < i) {

            cout << "* ";

            j++;

        }

  

        

        j = 0;

        i--;

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart2(n);

    return 0;

}

Output

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

5. Triangle Sample in C++

Methodology 1: Printing the given sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart2(int n)

{

    

    int i, j, ok = n;

  

    

    

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

  

        

        for (j = 1; j <= n; j++) {

  

            

            if (j >= ok)

                cout << "* ";

            else

                cout << " ";

        }

        k--;

        cout << "n";

    }

}

  

int primary()

{

    int n = 5;

    

    pypart2(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart2(int n)

{

    int i = 0, j = 0, ok = 0;

    whereas (i < n) {

  

        

        whereas (ok <= n - i - 2) {

            cout << " ";

            ok++;

        }

        ok = 0;

  

        

        whereas (j <= i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart2(n);

    return 0;

}

Output

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

6. Inverted Triangle Sample in C++

Methodology 1: Printing the inverted triangle sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void printInvTriangle(int n)

{

  

    

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

  

        

        int house = i;

  

        

        for (int j = 0; j < 2 * n - i - 1; j++) {

  

            

            

            if (house) {

                cout << "  ";

                space--;

            }

            else {

                cout << "* ";

            }

        }

        cout << endl;

    }

}

  

int primary()

{

    printInvTriangle(5);

  

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void printInvTriangle(int n)

{

  

    

    int i = 0;

    int j;

  

    

    whereas (i < n) {

  

        

        int house = i;

        j = 0;

  

        

        whereas (j < 2 * n - i - 1) {

  

            

            

            if (house) {

                cout << "  ";

                space--;

            }

            else {

                cout << "* ";

            }

            j++;

        }

        cout << endl;

        i++;

    }

}

  

int primary()

{

    printInvTriangle(5);

  

    return 0;

}

Output

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

6. Quantity Pyramid Sample in C++

Methodology 1: Printing the quantity pyramid sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void numpat(int n)

{

    

    int num = 1;

  

    

    

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

  

        

        

        for (int j = 0; j <= i; j++)

            cout << num << " ";

  

        

        num = num + 1;

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    numpat(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    int i = 1, j = 0;

    whereas (i <= n) {

        whereas (j <= i - 1) {

            cout << i << " ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart(n);

    return 0;

}

Output

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

7. Numbers Pyramid Sample in C++ with out Reassigning

Methodology 1: Printing the quantity pyramid sample in C++ with out reassigning utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void numpat(int n)

{

    

    int num = 1;

  

    

    

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

  

        

        

        for (int j = 0; j <= i; j++) {

  

            

            cout << num << " ";

  

            

            num = num + 1;

        }

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    numpat(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void pypart(int n)

{

    int i = 1, j = 0;

  

    

    

    int num = 1;

    whereas (i <= n) {

        whereas (j <= i - 1) {

  

            

            cout << num << " ";

  

            

            

            num++;

            j++;

        }

        j = 0;

        i++;

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    pypart(n);

    return 0;

}

Output

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

8. Steady Quantity Pyramid in C++ after 180° Rotation

C++

#embrace <iostream>

utilizing namespace std;

  

int primary()

{

    int rows = 5, rely = 0, count1 = 0, ok = 0;

  

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

        for (int house = 1; house <= rows - i; ++house) {

            cout << "  ";

            ++rely;

        }

  

        whereas (ok != 2 * i - 1) {

            if (rely <= rows - 1) {

                cout << i + ok << " ";

                ++rely;

            }

  

            ++ok;

        }

        count1 = rely = ok = 0;

  

        cout << endl;

    }

    return 0;

}

  

Output

        1 
      2 3 
    3 4 5 
  4 5 6 7 
5 6 7 8 9 

9. Palindrome Triangle Sample in C++

C++

#embrace <iostream>

utilizing namespace std;

  

int primary()

{

    int rows = 5, rely = 0, count1 = 0, ok = 0;

  

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

        for (int house = 1; house <= rows - i; ++house) {

            cout << "  ";

            ++rely;

        }

  

        whereas (ok != 2 * i - 1) {

            if (rely <= rows - 1) {

                cout << i + ok << " ";

                ++rely;

            }

            else {

                ++count1;

                cout << i + ok - 2 * count1 << " ";

            }

            ++ok;

        }

        count1 = rely = ok = 0;

  

        cout << endl;

    }

    return 0;

}

  

Output

        1 
      2 3 2 
    3 4 5 4 3 
  4 5 6 7 6 5 4 
5 6 7 8 9 8 7 6 5 

10. Alphabet Pyramid Sample in C++

Methodology 1: Printing the given sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void alphabet(int n)

{

    

    

    int num = 65;

  

    

    

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

  

        

        

        for (int j = 0; j <= i; j++) {

  

            

            char ch = char(num);

  

            

            cout << ch << " ";

        }

  

        

        num = num + 1;

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

    alphabet(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void alphabet(int n)

{

    int i = 1, j = 0;

  

    

    int num = 65;

  

    

    

    

    char alpha = char(num);

    whereas (i <= n) {

  

        

        

        

        whereas (j <= i - 1) {

            cout << alpha << " ";

            j++;

        }

  

        

        

        alpha++;

  

        

        

        

        j = 0;

        i++;

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    alphabet(n);

    return 0;

}

Output

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

11. Steady Alphabet Pyramid Sample in C++

Methodology 1: Printing the above sample utilizing for loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void contalpha(int n)

{

    

    

    int num = 65;

  

    

    

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

  

        

        

        for (int j = 0; j <= i; j++) {

  

            

            char ch = char(num);

  

            

            cout << ch << " ";

  

            

            num = num + 1;

        }

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    contalpha(n);

    return 0;

}

Output

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

Methodology 2: Printing the above sample utilizing whereas loop.

C++

#embrace <iostream>

utilizing namespace std;

  

void contalpha(int n)

{

    int i = 1, j = 0;

    int num = 65;

    char alpha = char(num);

    whereas (i <= n) {

        whereas (j <= i - 1) {

            cout << alpha << " ";

  

            

            

            

            alpha++;

            j++;

        }

        j = 0;

        i++;

  

        

        cout << endl;

    }

}

  

int primary()

{

    int n = 5;

  

    

    contalpha(n);

    return 0;

}

Output

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

Printing patterns in python language are mentioned within the following article – Packages for printing pyramid patterns in Python

This text is contributed by Manjeet Singh(S.Nandini). In the event you like GeeksforGeeks and want to contribute, you may also write an article utilizing write.geeksforgeeks.org. See your article showing on the GeeksforGeeks primary web page and assist different Geeks.
Please write feedback when you discover something incorrect, or if you wish to share extra details about the subject mentioned above.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments