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
Program to print hollow pyramid and diamond pattern in C++
In this article, we implement a C++ program to print a hollow pyramid and diamond pattern. We can create solid Pyramid patterns easily using a loop. To make it hollow, we have to add a few logics.
Hollow Pyramid
A hollow pyramid is a pattern where a pyramid shape is formed, but only the outer edges (the borders) are filled with characters, while the interior is left empty.
Let's explain how we can create hollow pyramid ?
- The first line prints only one star (*).
- The last line (i.e., the nth line) prints n stars in a row, forming the base of the pyramid.
-
For all the lines in between (2nd to n-1):
- The line starts with one star.
- Then, it has some spaces in the middle.
- Ends with another star.
C++ Program to Print Hollow Pyramid Pattern
Following is the C++ program to construct a hollow pyramid:
#include <iostream>
using namespace std;
int main() {
int n = 5;
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= (n - i); j++) {
//print the blank spaces before star
cout << " ";
}
if (i == 1 || i == n) {
//for the first and last line, print the stars continuously
for (j = 1; j <= i; j++) {
cout << "* ";
}
} else {
cout << "*";
//in each line star at start and end position
for (j = 1; j <= 2 * i - 3; j++) {
//print space to make hollow
cout << " ";
}
cout << "*";
}
cout << endl;
}
}
Following is the hollow pyramid ?
* * * * * * * * * * * *
Hollow Diamond
A hollow diamond is a pattern that creates a diamond shape, but only the edge is filled with character, leaving the interior empty.
The diamond pattern is made up with two symmetrical halves:
- The upper half (top to middle)
- The lower half (middle to bottom)
C++ Program to Print Hollow Diamond Pattern
The following is a C++ example to construct a hollow diamond ?
#include <iostream>
using namespace std;
int main() {
int n = 7;
int i, j, mid;
if (n % 2 == 1) {
//when n is odd, increase it by 1 to make it even
n++;
}
mid = (n / 2);
for (i = 1; i <= mid; i++) {
for (j = 1; j <= (mid - i); j++) {
//print the blank spaces before star
cout << " ";
}
if (i == 1) {
cout << "*";
} else {
cout << "*";
//in each line star at start and end position
for (j = 1; j <= 2 * i - 3; j++) {
//print space to make hollow
cout << " ";
}
cout << "*";
}
cout << endl;
}
for (i = mid + 1; i < n; i++) {
for (j = 1; j <= i - mid; j++) {
//print the blank spaces before star
cout << " ";
}
if (i == n - 1) {
cout << "*";
} else {
cout << "*";
//in each line star at start and end position
for (j = 1; j <= 2 * (n - i) - 3; j++) {
//print space to make hollow
cout << " ";
}
cout << "*";
}
cout << endl;
}
}
Following is the hollow diamond -
* * * * * * * * * * * *