C++ Program to Print Hollow Star Pyramid in a Diamond Shape

Creating a hollow star pyramid in a diamond shape is an interesting exercise to practice nested loops and conditional statements in C++. This pattern involves printing a hollow diamond made of stars (*), where the diamond consists of two parts: the upper pyramid and the inverted lower pyramid, both hollow except for their borders.

Example: Hollow Star Pyramid in a Diamond Shape

This program will prompt the user to enter the number of rows for the upper part of the diamond. The lower part will automatically be generated to match the upper part, creating a symmetrical diamond shape.

C++ Code:

#include <iostream>

using namespace std;

int main() {

    int rows;

    // User inputs the number of rows for the upper part of the diamond

    cout << “Enter the number of rows: “;

    cin >> rows;

    // Print the upper part of the diamond

    for (int i = 1; i <= rows; ++i) {

        // Print leading spaces

        for (int j = 1; j <= rows – i; ++j) {

            cout << ” “;

        }

        // Print stars and hollow spaces

        for (int j = 1; j <= 2 * i – 1; ++j) {

            if (j == 1 || j == 2 * i – 1) {

                cout << “*”;

            } else {

                cout << ” “;

            }

        }

        cout << endl;

    }

    // Print the lower part of the diamond

    for (int i = rows – 1; i > 0; –i) {

        // Print leading spaces

        for (int j = 1; j <= rows – i; ++j) {

            cout << ” “;

        }

        // Print stars and hollow spaces

        for (int j = 1; j <= 2 * i – 1; ++j) {

            if (j == 1 || j == 2 * i – 1) {

                cout << “*”;

            } else {

                cout << ” “;

            }

        }

        cout << endl;

    }

    return 0;

}

Explanation:

  1. Header and Namespace: Includes <iostream> for input and output operations and uses the std
  2. Input: The user specifies the number of rows for the upper half of the diamond.
  3. Upper Diamond:
    • Outer Loop: Manages the rows of the upper pyramid.
    • First Inner Loop: Controls the spaces before the stars begin, decreasing as the rows increase.
    • Second Inner Loop: Handles the printing of stars and hollow spaces. Stars are printed only at the edges (j == 1 or j == 2 * i – 1), leaving the rest as spaces.
  4. Lower Diamond:
    • Outer Loop: Manages the rows of the inverted lower pyramid.
    • First Inner Loop: Similar to the upper part but for spacing, it increases as you move deeper into the lower part.
    • Second Inner Loop: Similar to the upper part in terms of star placement and hollow spaces.

Program Output:

If the user inputs 4 for the number of rows, the output will be:

*

* *

*   *

*     *

*   *

* *

*

This pattern creates a visually appealing hollow diamond using nested loops and conditional checks to determine where to print stars and spaces, illustrating how to manipulate console output effectively in C++.

Leave a Reply

error: Content is protected !!