# Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets (A, B, C, D, E, F, G, H,…) to make forms like pyramids, triangles, and so on in this tutorial. The C++ pattern programs listed below, as well as instructions on how to run and print them, are covered in this tutorial.

1. Simple Half Pyramid Pattern of Stars Using While Loop
2. Simple Half Pyramid Pattern of Stars Using For Loop
3. Simple Half Inverted Pyramid Pattern of Stars
4. Simple Complete Pyramid Pattern of Stars
5. Simple Complete Inverted Pyramid Pattern of Stars
6. Simple Half Pyramid Pattern of Numbers Using Loop
7. Simple Half Inverted Pyramid of Numbers
8. Simple Complete Pyramid Pattern of Numbers
9. Pascal’s Triangle Pyramid of Numbers
10. Half Pyramid Pattern of Alphabets
11.  Full Pyramid Pattern of Alphabets

## Programs:

### 1. Print a Simple Half Pyramid Pattern of Stars Using While Loop

Code:

``````//CPP Code to Print Simple half Pyramid using while loop

#include <iostream>
using namespace std;

int main ()
{
//considering a variable
int j;

cout << "Enter the height of pyramid: ";

//storing value in variable j
cin >> j;

//considering a variable and initialize with 1
int row = 1;

//logic
while ( row <= j )
{
//taking a variable and initialize with 1
int col = 1;

//logic
while ( col <= row )
{
//printing star
cout << "*";

//incrementing col
col = col + 1;
}
cout << endl;

//incrementing row
row = row + 1;
}
}
``````

Output: (If User puts 7 as input)

``````Enter the height of pyramid: 7
*
**
***
****
*****
******
*******
``````

### 2. Print a Simple Half Pyramid Pattern of Stars Using For Loop

Code:

``````//CPP Code to Print Simple half Pyramid using for loop

#include <iostream>
using namespace std;

int main ()
{
//considering a variable
int row;

cout << "Enter the rows of pyramid: ";

//storing value in variable row
cin >> row;

//logic
for(int m = 1; m <= row; ++m)
{
for(int n = 1; n <= m; ++n)
{
//printing stars
cout << "*";
}
cout << "\n";
}
return 0;
}
``````

Output: (If the user puts 8 as input)

``````Enter the rows of pyramid: 8
*
**
***
****
*****
******
*******
********
``````

### 3. Print a Simple Half Inverted Pyramid of Stars

Code:

``````//CPP Code to Print Simple half inverted Pyramid

#include <iostream>
using namespace std;

int main ()
{
//considering a variable
int row;

cout << "Enter the rows of pyramid: ";

//storing value in variable row
cin >> row;

//logic
for(int m = 0; m <= row; m++)
{
for(int n = m; n <=  row; n++)
{
//printing stars
cout << "*";
}
cout << "\n";
}
return 0;
}
``````

Output: (If the user puts 6 as input)

``````Enter the rows of pyramid: 6
*******
******
*****
****
***
**
*
``````

### 4. Print Simple Complete Pattern Pyramid of Stars

Code:

``````//CPP Code to Print Simple complete inverted Pyramid
#include<iostream>
using namespace std;

int main()
{
//considering variables
int row, m, p, n;

cout << "Enter the number of rows: ";

//storing value in variable row
cin >> row;

//logic
for(m=1; m<=row; m++)
{
for( p= row; p > m; p-- )
cout<<" ";
for( n = 0; n < m; n++)

//printing stars
cout<<"* ";
cout << endl;
}
cout << endl;
return 0;
}
``````

Output: (If the user puts 5 as input)

``````Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
``````

### 5. Print Simple Complete Inverted Pattern Pyramid of Stars

Code:

``````//CPP Code to Print Simple complete inverted Pyramid
#include<iostream>
using namespace std;

int main()
{
//considering variables
int row, m, p, n;

cout << "Enter the number of rows: ";

//storing value in variable row
cin >> row;

//logic
for(m=1; m<=row; m++)
{
for( p= 1; p < m; p++ )
cout<<" ";
for( n = m; n <= row; n++)

//printing stars
cout<<"* ";
cout << endl;
}
cout << endl;
return 0;
}
``````

Output: (If user puts 11 as input)

``````Enter the number of rows: 11
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
``````

### 6. Simple Half Pyramid Pattern of Numbers Using Loop

Type 1:

``````//CPP Code to Print Simple half Pyramid using while loop

#include <iostream>
using namespace std;

int main ()
{
//considering a variable
int j;

cout << "Enter the height of pyramid: ";

//storing value in variable j
cin >> j;

//considering a variable and initialize with 1
int row = 1;

//logic
while ( row <= j )
{
//taking a variable and initialize with 1
int col = 1;

//logic
while ( col <= row )
{
//printing star
cout << row;

//incrementing col
col = col + 1;
}
cout << endl;

//incrementing row
row = row + 1;
}
}
``````

Output: (If the user puts 5 as input)

``````Enter the height of pyramid: 5
1
22
333
4444
55555
``````

Type 2:

``````//CPP Code to Print Simple half Pyramid using for loop
#include <iostream>
using namespace std;

int main()
{
//considering variable r
int r;

cout << "Enter the number of rows: ";

//storing value in r
cin >> r;

//logic
for(int m = 1; m <= r; ++m)
{
for(int n = 1; n <= m; ++n)
{
cout << n << " ";
}
cout << "\n";
}
return 0;
}
``````

Output: (If the user puts 6 as input)

``````Enter the number of rows: 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
``````

Type 3:

``````//CPP Code to Print Simple half Pyramid using while loop

#include <iostream>
using namespace std;

int main()
{
//taking variable h
int h;

cout << " Enter the height of pyramid : ";

//storing input
cin >> h;

int row = 1;

//logic
while ( row <= h)
{
int col = 1;
int val = row ;

while ( col <= row )
{
cout << val;
val ++;
col ++;
}
cout << endl;
row ++;
}
}
``````

Output: (If the user puts 4 as input)

``````Enter the height of pyramid : 4
1
23
345
4567
``````

Type 4:

``````//CPP Code to Print Simple half Pyramid using while loop

#include <iostream>
using namespace std;

int main()
{
//taking variable h
int h;

cout << " Enter the height of pyramid : ";

//storing input
cin >> h;

int m = 1;

//logic
while ( m <= h)
{
int n = 1;

while ( n <= m )
{
cout << ( m - n + 1 ) <<" ";
n ++;
}
cout << endl;
m ++;
}
}
``````

Output: (If user puts 5 as input)

``````Enter the height of pyramid : 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
``````

Type 5:

``````//CPP program to print half pyramid pattern

#include <iostream>
using namespace std;

int main()
{
//considering variables
int h, m, n, no = 1;

cout << "Enter the height of pyramid: ";

//storing value
cin >> h;

//logic
for(m = 1; m <= h; m++)
{
for(n = 1; n <= m; ++n)
{
cout << no << " ";
++no;
}

cout << endl;
}

return 0;
}
``````

Output: (If user puts 5 as input)

``````Enter the height of pyramid: 5
1
2 3
4 5 6
7 8 9 10
11 2 13 14 15
``````

### 7. Simple Half Inverted Pyramid Pattern of Numbers Using Loop

Code:

``````//CPP Code to Print Simple half inverted Pyramid using for loop

#include <iostream>
using namespace std;

int main()
{
//taking variable as h
int h;

//put a value
cout << "Enter height of pyramid: ";

//storing value
cin >> h;

//logic
for(int m = h; m >= 1; --m)
{
for(int n = 1; n <= m; ++n)
{
cout << n << " ";
}
cout << endl;
}

return 0;
}
``````

Output: (If user puts 5 as input)

``````Enter height of pyramid: 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
``````

### 8. Simple Complete Pyramid Pattern of Numbers

Type 1:

``````//CPP Code to Print Simple complete Pyramid

#include <iostream>
using namespace std;

int main()
{
//taking variables
int rs, c = 0, c1 = 0, m = 0;

cout << "Enter number of rows: ";

//storing value
cin >> rs;

//logic
for(int i = 1; i <= rs; ++i)
{
for(int sp = 1; sp <= rs-i; ++sp)
{
cout << "  ";
++ c;
}

while(m != 2 * i - 1)
{
if (c <= rs - 1)
{
cout << i + m << " ";
++ c;
}
else
{
++ c1;
cout << i + m - 2 * c1 << " ";
}
++m ;
}
c1 = c = m = 0;

cout << endl;
}
return 0;
}
``````

Output: (If the user puts 6 as input)

``````Enter number of rows: 6
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
6 7 8 9 10 11 10 9 8 7 6
``````

Type 2:

``````//CPP Code to Print Simple complete Pyramid

#include<iostream>
using namespace std;

int main()
{
//considering variable
int rs;

cout << "Enter the height of Pyramid:  ";

//storing value in variable rs
cin >> rs;

//logic
for (int m = 1; m <= rs; m++)
{
for (int n = rs; n > m; n--)
{
cout << " ";
}
for (int s = 1; s <= m; s++)
{
cout << m << " ";
}
cout << "\n";
}
}
``````

Output: (If user puts 5 as input)

``````Enter the height of Pyramid:  5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
``````

### 9. Pascal’s Triangle Pyramid of Numbers

Code:

``````#include <iostream>
using namespace std;

int main()
{
//considering variables
int h, pt = 1;

cout << "Enter number of rows: ";

//storing value in variable h
cin >> h;

//logic
for(int m = 0; m < h; m++)
{
for(int sp = 1; sp <= h-m; sp++)
cout <<"  ";

for(int n = 0; n <= m; n++)
{
if (n == 0 || m == 0)
pt = 1;
else
pt = pt * (m-n+1)/n;

cout << pt << "   ";
}
cout << endl;
}

return 0;
}
``````

Output: (If the user puts 6 as input)

``````Enter number of rows: 6
1
1   1
1   2   1
1   3   3   1
1   4   6   4   1
1   5   10   10   5   1
``````

### 10. Half Pyramid Pattern of Alphabets

Code:

``````//cpp to print half pyramid pattern of alphabets

#include <iostream>
using namespace std;

int main()
{
//considering variables
char ip, alph = 'A';

cout << "Enter any uppercase character to print in the end row: ";
//storing input
cin >> ip;

//logic
for(int m = 1; m <= (ip-'A'+1); ++m)
{
for(int n = 1; n <= m; ++n)
{
cout << alph << " ";
}
++alph;

cout << endl;
}
return 0;
}
``````

Output: (If user puts “F” as input)

``````Enter any uppercase character to print in the end row: F
A
B B
C C C
D D D D
E E E E E
F F F F F F
``````

### 11. Full Pyramid Pattern of Alphabets

Code:

``````//cpp to print full pyramid pattern of alphabets

#include <iostream>
using namespace std;

int main()
{
//taking variable
char alpha= 'A';
int sp, m, n, o, p;

cout << "Enter the height of Pyramid: ";

//storing value
cin >> sp;

//logic
for( m = 1; m <= sp; m++ )
{
for( n = sp; n >= m; n-- )
cout<<" ";
for(o = 1; o <= m; o++)
cout << alpha++;
alpha--;
for( p = 1; p < m; p++)
cout<<--alpha;
cout<<"\n";
alpha='A';
}
return 0;
}
``````

Output: (If user puts 5 as input)

``````Enter the height of Pyramid: 5
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
``````