C++ Program To Print The Diamond Shape Last Updated : 25 Jan, 2023 Suggest changes Share Like Article Like Report Given a number n, write a program to print a diamond shape with 2n-1 rows.Examples : Input: 5Output: C++ // C++ program to print diamond shape // with 2n-1 rows #include <bits/stdc++.h> using namespace std; // Prints diamond pattern with 2n-1 rows void printDiamond(int n) { int space = n - 1; // run loop (parent loop) // till number of rows for (int i = 0; i < n; i++) { // loop for initially space, // before star printing for (int j = 0;j < space; j++) cout << " "; // Print i+1 stars for (int j = 0; j <= i; j++) cout << "* "; cout << endl; space--; } // Repeat again in reverse order space = 0; // run loop (parent loop) // till number of rows for (int i = n-1; i > 0; i--) { // loop for initially space, // before star printing for (int j = 0; j < space; j++) cout << " "; // Print i stars for (int j = 0;j < i;j++) cout << " *"; cout << endl; space++; } } // Driver code int main() { printDiamond(5); return 0; } // This is code is contributed // by rathbhupendra Output * * * * * * * * * * * * * * * * * * * * * * * * * Time Complexity: O(n*n) since we are traversing rows and columns of a grid for printing spaces ' ' and star '*'.Auxiliary Space: O(1), No extra space used. Create Quiz K kartik Follow 0 Article Tags : C++ Programs C++ C Pattern Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like