C++ Program To Print Pyramid Patterns
In this article, we will discuss the following top pattern programs in C++ using star ( * ) , numbers or other characters.
Pyramid Patterns in C++
- Simple Pyramid Pattern
- Flipped Simple Pyramid Pattern
- Inverted Pyramid Pattern
- Flipped Inverted Pyramid Pattern
- Triangle Pattern
- Inverted Triangle Pattern
- Half Diamond Pattern
- Flipped Half Diamon Pattern
- Diamond Pattern
- Hourglass Pattern
- Number Pyramid Pattern
- Numbers Pyramid Pattern without Reassigning
- Continuous Number Pyramid Pattern after 180° Rotation
- Palindrome Triangle Pattern
- Alphabet Pyramid Pattern
- Continuous Alphabet Pyramid Pattern
C++ Programs to Print Patterns and Pyramids
1. Simple Pyramid Pattern in C++
Method 1: Printing simple pyramid pattern using for loop
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
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n^2), where n is the input number of rows.
Auxiliary Space: O(1),
Method 2: Printing the above pattern using while Loop
C++
#include <iostream>
using namespace std;
void pypart( int n)
{
int i = 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 above pattern using recursion.
Output
*
* *
* * *
* * * *
* * * * *
2. Flipped Simple Pyramid Pattern in C++
Method 1: Printing the 180° rotated simple pyramid pattern using for loop.
C++
#include <iostream>
using namespace std;
void pypart( int n)
{
int i = 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 2: Printing the above pattern using while loop.
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
*
* *
* * *
* * * *
* * * * *
3. Inverted Pyramid Pattern in C++
Method 1: Printing the pattern using for loop.
C++
#include <iostream>
using namespace std;
void pypart2( int n)
{
int k = 2 * n - 2;
for ( int i = n; i > 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 pattern using while loop.
C++
#include <iostream>
using namespace std;
void pypart2( int n)
{
int i = 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
* * * * *
* * * *
* * *
* *
*
Method 3: Printing the above pattern using recursion.
C++
#include <iostream>
using namespace std;
void pypart2( int n)
{
int i, j, k = n;
for (i = 1; i <= 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
* * * * *
* * * *
* * *
* *
*
4. Flipped Inverted Pyramid Pattern in C++
Method 1: Printing this pattern using for loop.
C++
#include <iostream>
using namespace std;
void pypart2( int n)
{
int i = 0, j = 0, k = 0;
while (i < n) {
while (k <= n - i - 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
* * * * *
* * * *
* * *
* *
*
Method 2: Printing the above pattern using while loop.
C++
< div id= "highlighter_670253" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" >< div class = "container" >< div class = "line number1 index0 alt2" ><code class = "comments" >
|
Output
* * * * *
* * * *
* * *
* *
*
5. Triangle Pattern in C++
Method 1: Printing the given pattern using for loop.
C++
#include <iostream>
using namespace std;
void pypart2( int n)
{
int i, j, k = n;
for (i = 1; i <= 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 above pattern using while loop.
C++
#include <iostream>
using namespace std;
void printInvTriangle( int n)
{
int i = 0;
int j;
while (i < n) {
int space = i;
j = 0;
while (j < 2 * n - i - 1) {
if (space) {
cout << " " ;
space--;
}
else {
cout << "* " ;
}
j++;
}
cout << endl;
i++;
}
}
int main()
{
printInvTriangle(5);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
6. Inverted Triangle Pattern in C++
Method 1: Printing the inverted triangle pattern using for loop.
C++
#include <iostream>
using namespace std;
void printRotatedTriangle( int n)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++) {
cout << "*" << " " ;
}
cout << endl;
}
for ( int i = 1; i <= n - 1; i++) {
for ( int j = 1; j <= n - i; j++) {
cout << "*" << " " ;
}
cout << endl;
}
}
int main()
{
int n = 5;
printRotatedTriangle(n);
return 0;
}
|
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Method 2: Printing the above pattern using while loop.
C++
#include <iostream>
using namespace std;
void printRotatedTriangle( int n)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n - i; j++) {
cout << " " ;
}
for ( int k = 1; k <= i; k++) {
cout << "*" ;
}
cout << endl;
}
for ( int i = 1; i <= n - 1; i++) {
for ( int j = 1; j <= i; j++) {
cout << " " ;
}
for ( int k = 1; k <= n - i; k++) {
cout << "*" ;
}
cout << endl;
}
}
int main()
{
int n = 5;
printRotatedTriangle(n);
return 0;
}
|
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
7. Half Diamond Pattern in C++
C++
#include <iostream>
using namespace std;
void Diamond( int n)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n - i; j++) {
cout << " " ;
}
for ( int k = 1; k <= 2 * i - 1; k++) {
cout << "*" ;
}
cout << endl;
}
for ( int i = 1; i <= n - 1; i++) {
for ( int j = 1; j <= i; j++) {
cout << " " ;
}
for ( int k = 1; k <= 2 * (n - i) - 1; k++) {
cout << "*" ;
}
cout << endl;
}
}
int main()
{
int n = 5;
Diamond(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
8. Flipped Half Diamond Pattern in C++
C++
#include <iostream>
using namespace std;
void printHourglass( int rows)
{
if (rows <= 0) {
return ;
}
for ( int i = 0; i < 2 * rows - 1; i++) {
if (i < rows) {
for ( int j = 0; j < 2 * rows - i - 1; j++) {
if (j < i) {
cout << " " ;
}
else {
cout << "* " ;
}
}
}
else {
for ( int j = 0; j < i + 1; j++) {
if (j < 2 * rows - i - 2) {
cout << " " ;
}
else {
cout << "* " ;
}
}
}
cout << endl;
}
cout << endl;
}
int main()
{
int rows = 5;
printHourglass(rows);
return 0;
}
|
Output
*
**
***
****
*****
****
***
**
*
9. Diamond Shaped Pattern in C++
C++
#include <iostream>
using 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 main()
{
int n = 5;
numpat(n);
return 0;
}
|
Output
*
***
*****
*******
*********
*******
*****
***
*
10. Hourglass Pattern in C++
C++
#include <iostream>
using namespace std;
void pypart( int n)
{
int i = 1, j = 0;
while (i <= n) {
while (j <= i - 1) {
cout << i << " " ;
j++;
}
j = 0;
i++;
cout << endl;
}
}
int main()
{
int n = 5;
pypart(n);
return 0;
}
|
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
11. Number Pyramid Pattern in C++
Method 1: Printing the number pyramid pattern using for loop.
C++
#include <iostream>
using 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 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 above pattern using while loop.
C++
#include <iostream>
using namespace std;
void pypart( int n)
{
int i = 1, j = 0;
int num = 1;
while (i <= n) {
while (j <= i - 1) {
cout << num << " " ;
num++;
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
12. Numbers Pyramid Pattern without Reassigning in C++
Method 1: Printing the number pyramid pattern in C++ without reassigning using for loop.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5, count = 0, count1 = 0, k = 0;
for ( int i = 1; i <= rows; ++i) {
for ( int space = 1; space <= rows - i; ++space) {
cout << " " ;
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
cout << i + k << " " ;
++count;
}
++k;
}
count1 = count = k = 0;
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 rows = 5, count = 0, count1 = 0, k = 0;
for ( int i = 1; i <= rows; ++i) {
for ( int space = 1; space <= rows - i; ++space) {
cout << " " ;
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
cout << i + k << " " ;
++count;
}
else {
++count1;
cout << i + k - 2 * count1 << " " ;
}
++k;
}
count1 = count = k = 0;
cout << endl;
}
return 0;
}
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
13. Continuous Number Pyramid after 180° Rotation in C++
C++
#include <iostream>
using 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 main()
{
int n = 5;
alphabet(n);
return 0;
}
|
Output
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
14. Palindrome Triangle Pattern in C++
C++
#include <iostream>
using namespace std;
void alphabet( int n)
{
int i = 1, j = 0;
int num = 65;
char alpha = char (num);
while (i <= n) {
while (j <= i - 1) {
cout << alpha << " " ;
j++;
}
alpha++;
j = 0;
i++;
cout << endl;
}
}
int main()
{
int n = 5;
alphabet(n);
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
15. Alphabet Pyramid Pattern in C++
Method 1: Printing the given pattern using for loop.
C++
#include <iostream>
using 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 main()
{
int n = 5;
contalpha(n);
return 0;
}
|
Output
A
B B
C C C
D D D D
E E E E E
Method 2: Printing the above pattern using while loop.
C++
#include <iostream>
using namespace std;
void contalpha( int n)
{
int i = 1, j = 0;
int num = 65;
char alpha = char (num);
while (i <= n) {
while (j <= i - 1) {
cout << alpha << " " ;
alpha++;
j++;
}
j = 0;
i++;
cout << endl;
}
}
int main()
{
int n = 5;
contalpha(n);
return 0;
}
|
Output
A
B B
C C C
D D D D
E E E E E
16. Continuous Alphabet Pyramid Pattern in C++
Method 1: Printing the above pattern using for loop.
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;
void contalpha( int n)
{
int i = 1, j = 0;
int num = 65;
char alpha = char (num);
while (i <= n) {
while (j <= i - 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 language are discussed in the following article – Programs for printing pyramid patterns in Python
Last Updated :
23 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...