C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern Last Updated : 10 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C++ // C++ program to print hollow diamond pattern #include <iostream> using namespace std; int main() { int n = 5, rows, columns; // for loop is used to identify the number of rows and // it is used to print upper triangle for (rows = 1; rows <= n; rows++) { // used for printing the spaces for (columns = n; columns > rows; columns--) { cout << " "; } // print star cout << "*"; // again print the spaces for (columns = 1; columns < (rows - 1) * 2; columns++) { cout << " "; } if (rows == 1) { cout << "\n"; } else { cout << "*\n"; } } // for loop is used to identify the number of rows and // it is used to print lower triangle for (rows = n - 1; rows >= 1; rows--) { // used for printing the spaces for (columns = n; columns > rows; columns--) { cout << " "; } // print star cout << "*"; for (columns = 1; columns < (rows - 1) * 2; columns++) { cout << " "; } if (rows == 1) { cout << "\n"; } else { cout << "*\n"; } } return 0; } Output * * * * * * * * * * * * * * * * Time complexity: O(n2) for given input n Auxiliary Space: O(1) 2. Using while loop: C++ // C++ program to print hollow diamond pattern #include <iostream> using namespace std; int main() { int n = 5, rows = 1, columns; // while loop is used to identify the number of rows and // it is used to print upper triangle while (rows <= n) { columns = n; // used for printing the spaces while (columns > rows) { cout << " "; columns--; } // print star cout << "*"; columns = 1; while (columns < (rows - 1) * 2) { cout << " "; columns++; } if (rows == 1) { cout << "\n"; } else { cout << "*\n"; } rows++; } // while loop is used to identify the number of rows and // it is used to print lower triangle rows = n - 1; while (rows >= 1) { columns = n; // used for printing the spaces while (columns > rows) { cout << " "; columns--; } // print star cout << "*"; columns = 1; while (columns < (rows - 1) * 2) { cout << " "; columns++; } if (rows == 1) { cout << "\n"; } else { cout << "*\n"; } rows--; } return 0; } Output * * * * * * * * * * * * * * * * Time complexity: O(rows*columns) Auxiliary Space: O(1) Create Quiz Comment L laxmigangarajula03 Follow 0 Improve L laxmigangarajula03 Follow 0 Improve Article Tags : C++ Programs C Language C Pattern Programs Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like