
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Programs To Create Pyramid and Pattern
In this article, we will show you how to write C++ programs to create different pyramids and patterns. You can create many kinds of patterns using stars, numbers, and alphabets.
Below is the list of patterns we will cover in this article-
- Simple Pyramid Pattern in C++
- Flipped Simple Pyramid Pattern in C++
- Inverted Pyramid Pattern in C++
- Flipped Inverted Pyramid Pattern in C++
- Triangle Pattern in C++
- Inverted Triangle Pattern in C++
- Half Diamond Pattern in C++
- Flipped Half Diamond Pattern in C++
- Diamond Shaped Pattern in C++
- Hourglass Pattern in C++
- Number Pyramid Pattern in C++
- Numbers Pyramid Pattern without Reassigning in C++
- Palindrome Triangle Pattern in C++
- Alphabet Pyramid Pattern in C++
- Continuous Alphabet Pyramid Pattern in C++
Simple Pyramid Pattern in C++
To make a simple pyramid pattern in C++, we use nested loops where the outer loop controls the number of rows and the inner loops manage the spaces and stars to form the pyramid shape.
C++ Program to Print Simple Pyramid Pattern
Below is a complete C++ program where we print a simple star pyramid using nested loops.
#include <iostream> using namespace std; int main() { int n = 5; for(int i = 1; i <= n; i++) { // Print stars increasing in each row for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a left-aligned simple pyramid of stars printed using nested loops.
* ** *** **** *****
Flipped Simple Pyramid Pattern in C++
To make a flipped simple pyramid pattern in C++, we use nested loops where the outer loop controls the number of rows and the inner loops first print spaces and then stars to align the pyramid to the right side.
C++ Program to Print Flipped Simple Pyramid
Below is a C++ program where we print the simple flipped pyramid using nested for loops.
#include <iostream> using namespace std; int main() { int n = 5; // Total rows for (int i = 1; i <= n; i++) { // Print spaces for (int s = 1; s <= n - i; s++) cout << " "; // Print stars for (int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The output below displays a vertically flipped pyramid made of asterisks.
* ** *** **** *****
Inverted Pyramid Pattern in C++
To create a left-aligned inverted pyramid pattern in C++, we start with the maximum number of stars in the first row and decrease the stars in each following row. We use nested loops where the outer loop controls the rows and the inner loop prints the stars for each row.
C++ Program to Print Inverted Pyramid
Below is a C++ program that prints a left-aligned inverted star pyramid using nested for loops.
#include <iostream> using namespace std; int main() { int n = 5; for(int i = n; i >= 1; i--) { // Print decreasing number of stars in each row for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a left-aligned inverted pyramid of stars printed using nested loops.
***** **** *** ** *
Flipped Inverted Pyramid Pattern in C++
This pattern is like the inverted pyramid but stars are right-aligned. We use nested loops where the outer loop controls rows, the first inner loop prints spaces, and the second inner loop prints stars.
C++ Program to Print Flipped Inverted Pyramid
Below is a C++ program that prints a right-aligned inverted pyramid using nested loops.
#include <iostream> using namespace std; int main() { int n = 5; for(int i = n; i >= 1; i--) { // Print leading spaces for right alignment for(int s = 1; s <= n - i; s++) cout << " "; // Print stars decreasing in each row for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a right-aligned inverted pyramid of stars printed using nested loops.
***** **** *** ** *
Triangle Pattern in C++
To make a triangle pattern in C++, we use nested loops where the outer loop controls the number of rows and the inner loop prints the stars, increasing the count in each row to form a left-aligned triangle.
C++ Program to Print Triangle Pattern
Below is a C++ program where we print a simple triangle using stars.
#include <iostream> using namespace std; int main() { int n = 5; // Number of rows for the pyramid for(int i = 1; i <= n; i++) { for(int s = 1; s <= n - i; s++) cout << " "; // Print stars to form the pyramid shape for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a simple pyramid created using asterisks.
* *** ***** ******* *********
Inverted Triangle Pattern in C++
To create a inverted triangle pattern in C++, we print the star pattern upside down. The outer loop runs from the given number down to 1, while the inner loops manage the spaces and stars.
C++ Program to Print Flipped Simple Pyramid
Below is a C++ program that prints an upside-down (flipped) pyramid of stars using nested for loops.
#include <iostream> using namespace std; int main() { int n = 5; for(int i = n; i >= 1; i--) { // Print spaces before the stars for(int s = 1; s <= n - i; s++) cout << " "; // Print stars to form the inverted pyramid for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } return 0; }
The output below displays a inverted triangle pattern made of asterisks ('*').
********* ******* ***** *** *
Half Diamond Pattern in C++
To make a half diamond pattern in C++, we use nested loops where the stars increase row by row up to the middle, then decrease. It's created by combining a triangle pattern followed by an inverted triangle.
C++ Program to Print Half Diamond Pattern
Below is a C++ program that prints a half diamond using stars.
#include <iostream> using namespace std; int main() { int n = 5; // First half: print increasing number of stars for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } // Second half: print decreasing number of stars for(int i = n - 1; i >= 1; i--) { for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The
* ** *** **** ***** **** *** ** *
Flipped Half Diamond Pattern in C++
To make a flipped half diamond pattern in C++, we use nested loops where the stars increase and then decrease, just like the half diamond, but each row is right-aligned using spaces before the stars.
C++ Program to Print Flipped Half Diamond
Below is a C++ program where we print a right-aligned half diamond pattern using nested loops.
#include <iostream> using namespace std; int main() { int n = 5; // Print top and bottom parts of the diamond with spaces for alignment for(int i = 1; i <= n; i++) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } for(int i = n - 1; i >= 1; i--) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a right-aligned half diamond star pattern printed using nested loops.
* ** *** **** ***** **** *** ** *
Diamond Shaped Pattern in C++
To make a diamond-shaped pattern in C++, we combine a full pyramid and an inverted pyramid using nested loops to print stars in a way that forms a diamond shape.
C++ Program to Print Diamond Pattern
Below is a C++ program where we prints a full diamond using stars.
#include <iostream> using namespace std; int main() { int n = 5; // Print the top half of the diamond with spaces and stars for(int i = 1; i <= n; i++) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } // Print the bottom half of the diamond similarly for(int i = n - 1; i >= 1; i--) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } return 0; }
The output below shows a full diamond star pattern made by printing increasing and then decreasing rows of stars, centered with spaces.
* *** ***** ******* ********* ******* ***** *** *
Hourglass Pattern in C++
To make an hourglass pattern in C++, we use nested loops to print stars starting wide at the top, narrowing to a point in the middle, then expanding again, forming a vertically flipped diamond shape.
C++ Program to Print Hourglass Pattern
Below is a complete C++ program that prints an hourglass pattern using stars with nested loops.
#include <iostream> using namespace std; int main() { int n = 4; // Print top inverted pyramid for(int i = n; i >= 1; i--) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } // Print bottom pyramid for(int i = 2; i <= n; i++) { for(int s = 1; s <= n - i; s++) cout << " "; for(int j = 1; j <= 2 * i - 1; j++) cout << "*"; cout << endl; } return 0; }
The output below shows an hourglass shape made of stars, created by printing a decreasing and then increasing pyramid with proper spacing.
******* ***** *** * *** ***** *******
Number Pyramid Pattern in C++
To make a number pyramid pattern in C++, we use nested loops where the outer loop controls the rows and the inner loop prints numbers that increase with each row to form a pyramid shape.
C++ Program to Print Number Pyramid Pattern
Below is a complete C++ program where we print a pyramid using nested loops.
#include <iostream> using namespace std; int main() { int n = 5; for (int i = 1; i <= n; i++) { // Print number i, i times for (int j = 1; j <= i; j++) { cout << i << " "; } cout << endl; } return 0; }
The output below shows a pyramid of numbers starting from 1 in each row, increasing up to the row number, centered with spaces.
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Numbers Pyramid Pattern without Reassigning
To make a number pyramid without resetting the numbers, we use nested loops to keep printing numbers in order from one row to the next without starting over.
C++ Program to Print Numbers Pyramid Without Reassigning
Below is a C++ program where we print a number pyramid with numbers that keep increasing from one row to the next.
#include <iostream> using namespace std; int main() { int n = 5; int num = 1; // Start counting from 1 for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << num << " "; num++; } cout << endl; } return 0; }
The output below shows a pyramid of continuously increasing numbers across all rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Palindrome Triangle Pattern
To make a palindrome triangle pattern, we use nested loops to print numbers that go up to a middle point and then come back down, making each row look the same from both sides.
C++ Program to Print Palindrome Triangle Pattern
Below is a C++ program where we prints a palindrome triangle of numbers.
#include <iostream> using namespace std; int main() { int n = 5; for (int i = 1; i <= n; i++) { // Print spaces for (int s = 1; s <= n - i; s++) cout << " "; // Print decreasing numbers for (int j = i; j >= 1; j--) cout << j; // Print increasing numbers for (int j = 2; j <= i; j++) cout << j; cout << endl; } return 0; }
The output below shows a pyramid where each row forms a palindrome sequence of numbers decreasing to 1 and then increasing back.
1 212 32123 4321234 543212345
Alphabet Pyramid Pattern
To make an alphabet pyramid, we use nested loops where each row prints letters in order, starting from A and forming a centered triangle shape.
C++ Program to Print Alphabet Pyramid Pattern
Below is a complete C++ program that prints a pyramid where each row repeats the same alphabet character based on the row.
#include <iostream> using namespace std; int main() { int n = 5; char ch = 'A'; for (int i = 1; i <= n; i++) { // Print the current character i times for (int j = 1; j <= i; j++) { cout << ch << " "; } cout << endl; ch++; // Move to next letter } return 0; }
The output below shows a pyramid of alphabets starting fresh from 'A' in each row, centered with spaces
A B B C C C D D D D E E E E E
Continuous Alphabet Pyramid Pattern
To make a continuous alphabet pyramid, we use nested loops to print letters one after another across the rows without starting over, creating a smooth flow of letters in a pyramid shape.
C++ Program to Print Continuous Alphabet Pyramid Pattern
Below is a complete C++ program that prints alphabets continuously across rows, creating a pyramid pattern.
#include <iostream> using namespace std; int main() { int n = 5; char ch = 'A'; for (int i = 1; i <= n; i++) { // Print i characters continuously for (int j = 1; j <= i; j++) { cout << ch << " "; ch++; if (ch > 'Z') ch = 'A'; // Wrap around if past 'Z' } cout << endl; } return 0; }
The output below shows a pyramid of alphabets continuing sequentially without resetting and centered with spaces.
A B C D E F G H I J K L M N O
Conclusion
In this article, we learned how to print different patterns in C++ using nested loops. From stars and numbers to alphabets, each pattern just needed a bit of logic with rows and spacing. Practicing these helps improve your understanding of loops and how to control output in C++.