Trends

C++ Program To Print Pyramid Patterns

Trending 1 year ago
beritaja.com

In this article, we will talk The pursuing top shape programs in C++ prima *, numbers, aliases different characters.

pyramid and patterns in c++

Pyramid Patterns in C++

  1. Simple Pyramid Pattern
  2. Simple Pyramid Pattern aft 180° Rotation
  3. Inverted Pyramid Pattern
  4. Inverted Pyramid Pattern aft 180° Rotation
  5. Triangle Pattern
  6. Inverted Triangle Pattern
  7. Number Pyramid Pattern
  8. Numbers Pyramid Pattern without Reassigning
  9. Continuous Number Pyramid Pattern aft 180° Rotation
  10. Palindrome Triangle Pattern
  11. Alphabet Pyramid Pattern
  12. Continuous Alphabet Pyramid Pattern

C++ Programs to Print Patterns and Pyramids

1. Simple Pyramid Pattern in C++

Method 1: Printing elemental pyramid shape utilizing for loop

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

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

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

            cout << "* ";

        }

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while Loop

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

    int one = 0, j = 0;

    while (i < n) {

        while (j <= i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Method 3: Printing The supra shape utilizing recursion.

C++

#include <iostream>

using namespace std;

void print(int n)

{

    if(n == 0)

    {

        return;

    }

      cout << "* "; 

      print(n - 1);

}

void pattern(int n)

{

    if (n == 0) 

    {

        return;

    }

    else 

    {

        pattern(n - 1);

          print(n);

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pattern(n);

}

Output

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

2. Simple Pyramid Pattern in C++ aft 180° Rotation

Method 1: Printing The 180° rotated elemental pyramid shape utilizing for loop.

C++

#include <iostream>

using namespace std;

int main()

{

    int n = 5;

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

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

        {

            if (j >= i) {

                cout << "* ";

            }

            else {

                cout << "  ";

            }

        }

        cout << endl;

    }

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart2(int n)

{

    int one = 0, j = 0, k = 0;

    while (i < n) {

        while (k < (n - one - 1)) {

            cout << "  ";

            k++;

        }

        k = 0;

        while (j <= i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart2(n);

    return 0;

}

Output

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

3. Inverted Pyramid Pattern in C++

Method 1: Printing The shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

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

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

            cout << "* ";

        }

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Method 2: Printing The shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

    int one = n, j = 0;

    while (i > 0) {

        while (j < i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i--;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

Method 3: Printing The supra shape utilizing recursion.

C++

#include <bits/stdc++.h>

using namespace std;

void print(int n) 

{

      if(n == 0)

    {

        return;

    }

      cout << "* ";

      print(n - 1);

}

void pattern(int n)

{

    if (n == 0) {

        return;

    }

       print(n);

    cout << endl;

    pattern(n - 1);

}

int main()

{

    int n = 5;

    pattern(n);

    return 0;

}

Output

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

4. Inverted Pyramid Pattern in C++ aft 180° Rotation 

Method 1: Printing this shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void pypart2(int n)

{

    int k = 2 * n - 2;

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

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

            cout << "  ";

        k = k - 2;

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

            cout << "* ";

        }

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart2(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart2(int n)

{

    int one = n, j = 0, k = 0;

    while (i > 0) {

        while (k < (n - i)) {

            cout << "  ";

            k++;

        }

        k = 0;

        while (j < i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i--;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart2(n);

    return 0;

}

Output

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

5. Triangle Pattern in C++

Method 1: Printing The fixed shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void pypart2(int n)

{

    int i, j, k = n;

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

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

            if (j >= k)

                cout << "* ";

            else

                cout << " ";

        }

        k--;

        cout << "\n";

    }

}

int main()

{

    int n = 5;

    pypart2(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart2(int n)

{

    int one = 0, j = 0, k = 0;

    while (i < n) {

        while (k <= n - one - 2) {

            cout << " ";

            k++;

        }

        k = 0;

        while (j <= i) {

            cout << "* ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart2(n);

    return 0;

}

Output

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

6. Inverted Triangle Pattern in C++

Method 1: Printing The inverted triangle shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void printInvTriangle(int n)

{

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

        int abstraction = i;

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

            if (space) {

                cout << "  ";

                space--;

            }

            else {

                cout << "* ";

            }

        }

        cout << endl;

    }

}

int main()

{

    printInvTriangle(5);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void printInvTriangle(int n)

{

    int one = 0;

    int j;

    while (i < n) {

        int abstraction = i;

        j = 0;

        while (j < 2 * n - one - 1) {

            if (space) {

                cout << "  ";

                space--;

            }

            else {

                cout << "* ";

            }

            j++;

        }

        cout << endl;

        i++;

    }

}

int main()

{

    printInvTriangle(5);

    return 0;

}

Output

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

6. Number Pyramid Pattern in C++

Method 1: Printing The number pyramid shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void numpat(int n)

{

    int num = 1;

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

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

            cout << num << " ";

        num = num + 1;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    numpat(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

    int one = 1, j = 0;

    while (i <= n) {

        while (j <= one - 1) {

            cout << one << " ";

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    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 Pattern in C++ without Reassigning

Method 1: Printing The number pyramid shape in C++ without reassigning utilizing for loop.

C++

#include <iostream>

using namespace std;

void numpat(int n)

{

    int num = 1;

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

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

            cout << num << " ";

            num = num + 1;

        }

        cout << endl;

    }

}

int main()

{

    int n = 5;

    numpat(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void pypart(int n)

{

    int one = 1, j = 0;

    int num = 1;

    while (i <= n) {

        while (j <= one - 1) {

            cout << num << " ";

            num++;

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    pypart(n);

    return 0;

}

Output

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

8. Continuous Number Pyramid in C++ aft 180° Rotation

C++

#include <iostream>

using namespace std;

int main()

{

    int rows = 5, count = 0, count1 = 0, k = 0;

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

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

            cout << "  ";

            ++count;

        }

        while (k != 2 * one - 1) {

            if (count <= rows - 1) {

                cout << one + k << " ";

                ++count;

            }

            ++k;

        }

        count1 = count = k = 0;

        cout << endl;

    }

    return 0;

}

Output

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

9. Palindrome Triangle Pattern in C++

C++

#include <iostream>

using namespace std;

int main()

{

    int rows = 5, count = 0, count1 = 0, k = 0;

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

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

            cout << "  ";

            ++count;

        }

        while (k != 2 * one - 1) {

            if (count <= rows - 1) {

                cout << one + k << " ";

                ++count;

            }

            else {

                ++count1;

                cout << one + k - 2 * count1 << " ";

            }

            ++k;

        }

        count1 = count = k = 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 Pattern in C++

Method 1: Printing The fixed shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void alphabet(int n)

{

    int num = 65;

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

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

            char ch = char(num);

            cout << ch << " ";

        }

        num = num + 1;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    alphabet(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void alphabet(int n)

{

    int one = 1, j = 0;

    int num = 65;

    char alpha = char(num);

    while (i <= n) {

        while (j <= one - 1) {

            cout << alpha << " ";

            j++;

        }

        alpha++;

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    alphabet(n);

    return 0;

}

Output

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

11. Continuous Alphabet Pyramid Pattern in C++

Method 1: Printing The supra shape utilizing for loop.

C++

#include <iostream>

using namespace std;

void contalpha(int n)

{

    int num = 65;

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

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

            char ch = char(num);

            cout << ch << " ";

            num = num + 1;

        }

        cout << endl;

    }

}

int main()

{

    int n = 5;

    contalpha(n);

    return 0;

}

Output

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

Method 2: Printing The supra shape utilizing while loop.

C++

#include <iostream>

using namespace std;

void contalpha(int n)

{

    int one = 1, j = 0;

    int num = 65;

    char alpha = char(num);

    while (i <= n) {

        while (j <= one - 1) {

            cout << alpha << " ";

            alpha++;

            j++;

        }

        j = 0;

        i++;

        cout << endl;

    }

}

int main()

{

    int n = 5;

    contalpha(n);

    return 0;

}

Output

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

Printing patterns in python connection are discussed in The pursuing article – Programs for printing pyramid patterns in Python

This article is contributed by Manjeet Singh(S.Nandini). If you for illustration GeeksforGeeks and would for illustration to contribute, you Can besides constitute an article utilizing write.geeksforgeeks.org. See your article appearing connected The GeeksforGeeks main page and thief different Geeks.
Please constitute comments if you find thing incorrect, aliases if you want to stock much accusation astir The taxable discussed above.


Editor: Naga



Read other contents from Beritaja.com at
More Source
close