On this article, we are going to talk about the next high sample applications in C++ star *, numbers, or different characters.
Pyramid Patterns in C++
- Easy Pyramid Sample
- Easy Pyramid Sample after 180° Rotation
- Inverted Pyramid Sample
- Inverted Pyramid Sample after 180° Rotation
- Triangle Sample
- Inverted Triangle Sample
- Quantity Pyramid Sample
- Numbers Pyramid Sample with out Reassigning
- Steady Quantity Pyramid Sample after 180° Rotation
- Palindrome Triangle Sample
- Alphabet Pyramid Sample
- Steady Alphabet Pyramid Sample
C++ Applications to Print Patterns and Pyramids
1. Easy Pyramid Sample in C++
Technique 1: Printing easy pyramid sample utilizing for loop
C++
#embody <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 predominant()
{
int n = 5;
pypart(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Technique 2: Printing the above sample utilizing whereas Loop
C++
#embody <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 predominant()
{
int n = 5;
pypart(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Technique 3: Printing the above sample utilizing recursion.
C++
#embody <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 predominant()
{
int n = 5;
sample(n);
}
|
Output
*
* *
* * *
* * * *
* * * * *
2. Easy Pyramid Sample in C++ after 180° Rotation
Technique 1: Printing the 180° rotated easy pyramid sample utilizing for loop.
C++
#embody <iostream>
utilizing namespace std;
int predominant()
{
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
*
* *
* * *
* * * *
* * * * *
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart2(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
3. Inverted Pyramid Sample in C++
Technique 1: Printing the sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart(n);
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Technique 2: Printing the sample utilizing whereas loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart(n);
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Technique 3: Printing the above sample utilizing recursion.
C++
#embody <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 predominant()
{
int n = 5;
sample(n);
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
4. Inverted Pyramid Sample in C++ after 180° Rotation
Technique 1: Printing this sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart2(n);
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart2(n);
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
5. Triangle Sample in C++
Technique 1: Printing the given sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart2(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
int n = 5;
pypart2(n);
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
6. Inverted Triangle Sample in C++
Technique 1: Printing the inverted triangle sample utilizing for loop.
C++
#embody <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 predominant()
{
printInvTriangle(5);
return 0;
}
|
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
printInvTriangle(5);
return 0;
}
|
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
6. Quantity Pyramid Sample in C++
Technique 1: Printing the quantity pyramid sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
numpat(n);
return 0;
}
|
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
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
Technique 1: Printing the quantity pyramid sample in C++ with out reassigning utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
numpat(n);
return 0;
}
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
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++
#embody <iostream>
utilizing namespace std;
int predominant()
{
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++
#embody <iostream>
utilizing namespace std;
int predominant()
{
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++
Technique 1: Printing the given sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
alphabet(n);
return 0;
}
|
Output
A
B B
C C C
D D D D
E E E E E
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
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++
Technique 1: Printing the above sample utilizing for loop.
C++
#embody <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 predominant()
{
int n = 5;
contalpha(n);
return 0;
}
|
Output
A
B C
D E F
G H I J
Ok L M N O
Technique 2: Printing the above sample utilizing whereas loop.
C++
#embody <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 predominant()
{
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 – Applications for printing pyramid patterns in Python
This text is contributed by Manjeet Singh(S.Nandini). If you happen to like GeeksforGeeks and want to contribute, you may also write an article utilizing write.geeksforgeeks.org. See your article showing on the GeeksforGeeks predominant web page and assist different Geeks.
Please write feedback in the event you discover something incorrect, or if you wish to share extra details about the subject mentioned above.