C++ Program To Print Pyramid Patterns
In this article, we will discuss the following top pattern programs in C++ star *, numbers, or other characters.

Pyramid Patterns in C++
- Simple Pyramid Pattern
- Simple Pyramid Pattern after 180° Rotation
- Inverted Pyramid Pattern
- Inverted Pyramid Pattern after 180° Rotation
- Triangle Pattern
- Inverted Triangle 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++
// 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; } |
* * * * * * * * * * * * * * *
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++
// 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 int i = 0, j = 0; while (i < n) { // Inner loop to handle number of columns // values changing acc. to outer loop while (j <= i) { // Printing stars cout << "* " ; j++; } j = 0; // we have to reset j value so as it can // start from beginning and print * equal to // i. i++; // Ending line after each row cout << endl; } } // Driver Code int main() { int n = 5; pypart(n); return 0; } |
* * * * * * * * * * * * * * *
Method 3: Printing the above pattern using recursion.
C++
// C++ code to demonstrate star pattern using recursion #include <iostream> using namespace std; // recursive function to print * on console void print( int n) { if (n == 0) // base case { return ; } cout << "* " ; print(n - 1); // calling recursively } // recursive function to demonstrate printing pattern void pattern( int n) { // base case if (n == 0) { return ; } else { // calling function recursively pattern(n - 1); // print "* " n times print(n); // Ending line after each row cout << endl; } } // Driver Function int main() { int n = 5; pattern(n); } // this code is contributed by Shivesh Kumar Dwivedi |
* * * * * * * * * * * * * * *
2. Simple Pyramid Pattern in C++ after 180° Rotation
Method 1: Printing the 180° rotated simple pyramid pattern using for loop.
C++
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Driver Code int main() { int n = 5; // looping rows for ( int i = n; i > 0; i--) { for ( int j = 1; j <= n; j++) // looping columns { if (j >= i) { cout << "* " ; } else { cout << " " ; } } cout << endl; } return 0; } |
* * * * * * * * * * * * * * *
Method 2: Printing the above pattern using while loop.
C++
// C++ code to demonstrate pattern printing #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart2( int n) { int i = 0, j = 0, k = 0; while (i < n) { // for number of spaces while (k < (n - i - 1)) { cout << " " ; k++; } // resetting k because we want to run k from // beginning. k = 0; while (j <= i) { cout << "* " ; j++; } // resetting k so as it can start from 0. j = 0; i++; cout << endl; } } // Driver Code int main() { int n = 5; // Function Call pypart2(n); return 0; } |
* * * * * * * * * * * * * * *
3. Inverted Pyramid Pattern in C++
Method 1: Printing the pattern using for loop.
C++
// 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 = n; i > 0; 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; } |
* * * * * * * * * * * * * * *
Method 2: Printing the pattern using while loop.
C++
// 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 int i = n, j = 0; while (i > 0) { // Inner loop to handle number of columns // values changing acc. to outer loop while (j < i) { // Printing stars cout << "* " ; j++; } j = 0; // we have to reset j value so as it can // start from beginning and print * equal to // i. i--; // Ending line after each row cout << endl; } } // Driver Code int main() { int n = 5; pypart(n); return 0; } |
* * * * * * * * * * * * * * *
Method 3: Printing the above pattern using recursion.
C++
// C++ code to print Inverted Pyramid Pattern using // recursion #include <bits/stdc++.h> using namespace std; // recursive function for printing void print( int n) { if (n == 0) // base case { return ; } cout << "* " ; print(n - 1); // recursive calling for printing } void pattern( int n) { if (n == 0) { return ; } print(n); cout << endl; pattern(n - 1); } // driver function int main() { int n = 5; pattern(n); return 0; } // This code is contributed by Shivesh Kumar Dwivedi |
* * * * * * * * * * * * * * *
4. Inverted Pyramid Pattern in C++ after 180° Rotation
Method 1: Printing this pattern using for loop.
C++
// 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 = n; i > 0; i--) { // Inner loop to handle number spaces // values changing acc. to requirement for ( int j = 0; j < n - i; 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 Code int main() { int n = 5; // Function Call pypart2(n); return 0; } |
* * * * * * * * * * * * * * *
Method 2: Printing the above pattern using while loop.
C++
// C++ code to demonstrate pattern printing #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart2( int n) { int i = n, j = 0, k = 0; while (i > 0) { // for number of spaces while (k < (n - i)) { cout << " " ; k++; } // resetting k because we want to run k from // beginning. k = 0; while (j < i) { cout << "* " ; j++; } // resetting k so as it can start from 0. j = 0; i--; cout << endl; } } // Driver Code int main() { int n = 5; // Function Call pypart2(n); return 0; } |
* * * * * * * * * * * * * * *
5. Triangle Pattern in C++
Method 1: Printing the given pattern using for loop.
C++
// 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 Code int main() { int n = 5; // Function Call pypart2(n); return 0; } |
* * * * * * * * * * * * * * *
Method 2: Printing the above pattern using while loop.
C++
// C++ code to demonstrate star pattern #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart2( int n) { int i = 0, j = 0, k = 0; while (i < n) { // for spacing while (k <= n - i - 2) { cout << " " ; k++; } k = 0; // For Pattern printing while (j <= i) { cout << "* " ; j++; } j = 0; i++; cout << endl; } } // Driver Code int main() { int n = 5; // Function Call pypart2(n); return 0; } |
* * * * * * * * * * * * * * *
6. Inverted Triangle Pattern in C++
Method 1: Printing the inverted triangle pattern using for loop.
C++
// C++ Program to print the inverted triangle pattern #include <iostream> using namespace std; // function to print inverted triangle void printInvTriangle( int n) { // outer loop to go thruough every row for ( int i = 0; i < n; i++) { // leading space counter int space = i; // inner loop to print space and star * in each row for ( int j = 0; j < 2 * n - i - 1; j++) { // condition to check weather the star * or // whitespace is to be printed if (space) { cout << " " ; space--; } else { cout << "* " ; } } cout << endl; } } // driver code int main() { printInvTriangle(5); return 0; } |
* * * * * * * * * * * * * * * * * * * * * * * * *
Method 2: Printing the above pattern using while loop.
C++
// C++ program to print inverted triangle pattern using // while loop #include <iostream> using namespace std; // function to print pattern void printInvTriangle( int n) { // loop variables int i = 0; int j; // outer loop to iterate through each row while (i < n) { // leading space counter int space = i; j = 0; // inner loop to print pattern in each row while (j < 2 * n - i - 1) { // condition to check weather to print star * or // whitespace if (space) { cout << " " ; space--; } else { cout << "* " ; } j++; } cout << endl; i++; } } // driver code int main() { printInvTriangle(5); return 0; } |
* * * * * * * * * * * * * * * * * * * * * * * * *
6. Number Pyramid Pattern in C++
Method 1: Printing the number pyramid pattern using for loop.
C++
// 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 Code int main() { int n = 5; // Function Call numpat(n); return 0; } |
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++
// C++ Code for pattern Printing #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart( int n) { int i = 1, j = 0; while (i <= n) { while (j <= i - 1) { cout << i << " " ; j++; } j = 0; i++; cout << endl; } } // Driver Code int main() { int n = 5; // Function Call pypart(n); return 0; } |
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
7. Numbers Pyramid Pattern in C++ without Reassigning
Method 1: Printing the number pyramid pattern in C++ without reassigning using for loop.
C++
// 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 Code int main() { int n = 5; // Function Call numpat(n); return 0; } |
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++
// C++ code to demonstrate printing pattern of numbers #include <iostream> using namespace std; // Function to demonstrate printing pattern void pypart( int n) { int i = 1, j = 0; // here we declare an num variable which is // assigned value 1 int num = 1; while (i <= n) { while (j <= i - 1) { // Printing numbers cout << num << " " ; // here we are increasing num for every // iteration. num++; j++; } j = 0; i++; // Ending line after each row cout << endl; } } // Driver Code int main() { int n = 5; // Function Call pypart(n); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
8. Continuous Number Pyramid in C++ after 180° Rotation
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; } // code by Kashif RB |
1 2 3 3 4 5 4 5 6 7 5 6 7 8 9
9. Palindrome Triangle Pattern in C++
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; } // code by Kashif Rb |
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 Pattern in C++
Method 1: Printing the given pattern using for loop.
C++
// C++ code to demonstrate printing pattern of alphabets #include <iostream> using namespace std; // Function to demonstrate printing pattern void alphabet( 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; alphabet(n); return 0; } |
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++
// C++ code to demonstrate printing pattern of alphabets #include <iostream> using namespace std; // Function to demonstrate printing pattern void alphabet( int n) { int i = 1, j = 0; // assigning ASCII value of A which is 65 int num = 65; // converting ASCII value to character, // now our alpha variable is having // value A after typecasting. char alpha = char (num); while (i <= n) { // alpha is having A value and it // will change as soon as alpha // increased or decreased. while (j <= i - 1) { cout << alpha << " " ; j++; } // incrementing alpha value so as it can // point to next character alpha++; // we have to reset j value so as it can // start from beginning and print * equal to // i. j = 0; i++; // Ending line after each row cout << endl; } } // Driver Code int main() { int n = 5; // Function Call alphabet(n); return 0; } |
A B B C C C D D D D E E E E E
11. Continuous Alphabet Pyramid Pattern in C++
Method 1: Printing the above pattern using for loop.
C++
// 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++) { // Explicitly 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 Code int main() { int n = 5; // Function Call contalpha(n); return 0; } |
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++
// C++ code to demonstrate printing pattern of alphabets #include <iostream> using namespace std; // Function to demonstrate printing pattern 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 << " " ; // incrementing alpha value in every // iteration so as it can assign to // next character alpha++; j++; } j = 0; i++; // Ending line after each row cout << endl; } } // Driver Code int main() { int n = 5; // Function Call contalpha(n); return 0; } |
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
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 write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Write a program to print the pyramid pattern formed of stars
Example
input n=10
output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Approach: using nested loop
The approach used here is to first print a certain number of spaces based on the current row number (i) and the total number of rows (n) to make sure that the pattern is aligned to the right. Then, the required number of asterisks are printed in the current row using a nested loop.
Algorithm
1. Take input for the number of rows (n)
2. Iterate through each row from 1 to n
3. Print spaces before the asterisks for alignment
4. Iterate through each column from 1 to i
5. Print an asterisk followed by a space
6. Move to the next line for the next row
Python3
n = 10 for i in range ( 1 ,n + 1 ): print ( " " * (n - i),end = "") for j in range ( 1 ,i + 1 ): print ( "*" ,end = " " ) print () |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Time Complexity:
The time complexity of this program is O(n^2), as it involves two nested loops, one for the rows and another for the columns.
Space Complexity:
The space complexity of this program is O(1), as it uses a constant amount of space to print each character.
Please Login to comment...