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; } |
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 = 10; pypart2(n); return 0; } |
Output:
*
* *
* * *
* * * *
* * * * *
Method 1
// 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; } |
Method 2
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart2(int n) { // Number of spaces int i, j, k = n; // Outer loop to handle number of rows // n in this case for (i = 1; i <= n; i++) { // Inner loop for columns for (j = 1; j <= n; j++) { // Condition to print star pattern if (j >= k) cout << "* "; else cout << " "; } k--; cout << "\n"; } } // Driver Function int main() { int n = 5; pypart2(n); return 0; } |
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; } |
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; } |
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; } |
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; } |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Programs for printing pyramid patterns in Python
- Programs for printing pyramid patterns in Java
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns using recursion
- Programs to print Interesting Patterns
- Programs to print Triangle and Diamond patterns using recursion
- Program to print pyramid pattern
- Print the following pyramid pattern
- Program to print hollow pyramid and diamond pattern
- C Program to print pyramid pattern
- Program for volume of Pyramid
- Program to print a inverse pyramid character pattern
- Print the pyramid pattern with given height and minimum number of stars
- Program to print Even Odd Number Pyramid
- Program to Print Pyramid Pattern using numbers
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Printing Heart Pattern in C
- Pattern Printing question asked in CGI Coding Round
- Inner reducing pattern printing
- Printing string in plus ‘+’ pattern in the matrix
Improved By : Raghav12Singh

