Programs for printing pyramid patterns in C++
This article is aimed at giving a C++ implementation for pattern printing.
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart(int n) { // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for(int j=0; j<=i; j++ ) { // printing stars cout << "* "; } // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; pypart(n); return 0; } |
chevron_right
filter_none
Output:
* * * * * * * * * * * * * * *
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart2(int n) { // number of spaces int k = 2*n - 2; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for (int j=0; j<k; j++) cout <<" "; // decrementing k after each loop k = k - 2; // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // printing stars cout << "* "; } // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; pypart2(n); return 0; } |
chevron_right
filter_none
Output:
*
* *
* * *
* * * *
* * * * *
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Function to demonstrate printing pattern void triangle(int n) { // number of spaces int k = 2*n - 2; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for (int j=0; j<k; j++) cout <<" "; // decrementing k after each loop k = k - 1; // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // printing stars cout << "* "; } // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; triangle(n); return 0; } |
chevron_right
filter_none
Output:
*
* *
* * *
* * * *
* * * * *
// C++ code to demonstrate printing pattern of numbers #include <iostream> using namespace std; // Function to demonstrate printing pattern void numpat(int n) { // initializing starting number int num = 1; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) cout << num << " "; // incrementing number at each column num = num + 1; // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; numpat(n); return 0; } |
chevron_right
filter_none
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
// C++ code to demonstrate printing pattern of numbers #include <iostream> using namespace std; // Function to demonstrate printing pattern void numpat(int n) { // initialising starting number int num = 1; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // printing number cout << num << " "; // incrementing number at each column num = num + 1; } // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; numpat(n); return 0; } |
chevron_right
filter_none
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// C++ code to demonstrate printing pattern of alphabets #include <iostream> using namespace std; // Function to demonstrate printing pattern void alphapat(int n) { // initializing value corresponding to 'A' // ASCII value int num = 65; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // explicitly converting to char char ch = char(num); // printing char value cout << ch << " "; } // incrementing number num = num + 1; // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; alphapat(n); return 0; } |
chevron_right
filter_none
Output:
A B B C C C D D D D E E E E E
// C++ code to demonstrate printing pattern of alphabets #include <iostream> using namespace std; // Function to demonstrate printing pattern void contalpha(int n) { // initializing value corresponding to 'A' // ASCII value int num = 65; // outer loop to handle number of rows // n in this case for (int i=0; i<n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for (int j=0; j<=i; j++ ) { // explicitely converting to char char ch = char(num); // printing char value cout << ch << " "; // incrementing number at each column num = num + 1; } // ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; contalpha(n); return 0; } |
chevron_right
filter_none
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 article below :
Programs for printing pyramid patterns in Python
This article is contributed by Manjeet Singh(S.Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns in Java
- Programs for printing pyramid patterns in Python
- Programs for printing pyramid patterns using recursion
- Programs to print Interesting Patterns
- Programs to print Triangle and Diamond patterns using recursion
- Program for volume of Pyramid
- Print the following pyramid pattern
- Program to print Even Odd Number Pyramid
- Program to print pyramid pattern
- Program to print pyramid pattern
- Program to Print Pyramid Pattern using numbers
- Program to print a inverse pyramid character pattern
- Program to print hollow pyramid and diamond pattern
- Print the pyramid pattern with given height and minimum number of stars



