Code of hollow diamond in cpp language

Computer Science

ScanSolve AI Answer

Step-by-step solution

5 steps

Here's a C++ code to print a hollow diamond pattern:

#include <iostream>

int main() {
    int size;

    std::cout << "Enter the size of the diamond (odd number recommended): ";
    std::cin >> size;

    // Ensure size is odd for a symmetrical diamond
    if (size % 2 == 0) {
        size++;
        std::cout << "Size adjusted to the next odd number: " << size << std::endl;
    }

    int midRow = size / 2;

    // Upper half of the diamond
    for (int i = 0; i <= midRow; ++i) {
        // Print leading spaces
        for (int j = 0; j < midRow - i; ++j) {
            std::cout << " ";
        }
        // Print the first star
        std::cout << "*";
        // Print spaces between stars for the hollow part
        if (i > 0) {
            for (int j = 0; j < 2 * i - 1; ++j) {
                std::cout << " ";
            }
            // Print the second star
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    // Lower half of the diamond
    for (int i = midRow - 1; i >= 0; --i) {
        // Print leading spaces
        for (int j = 0; j < midRow - i; ++j) {
            std::cout << " ";
        }
        // Print the first star
        std::cout << "*";
        // Print spaces between stars for the hollow part
        if (i > 0) {
            for (int j = 0; j < 2 * i - 1; ++j) {
                std::cout << " ";
            }
            // Print the second star
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    return 0;
}

Explanation:

  1. Include Header

    The iostream header is included for input/output operations.

  2. Get Input

    The program prompts the user to enter a size for the diamond. It's recommended to use an odd number for a symmetrical diamond. If an even number is entered, it's automatically incremented to the next odd number.

  3. midRow Calculation

    midRow is calculated as size / 2. This represents the row index of the widest part of the diamond.

  4. Upper Half

    • The outer loop iterates from i = 0 to midRow.
    • Leading Spaces: The first inner loop prints the spaces before the stars. The number of spaces decreases as i increases.
    • First Star: A star * is printed.
    • Hollow Part: For rows other than the top row (i > 0), spaces are printed between the two stars to create the hollow effect. The number of spaces is 2 * i - 1.
    • Second Star: Another star * is printed if it's not the top row.
    • Newline: std::endl moves the cursor to the next line.
  5. Lower Half

    • The outer loop iterates from i = midRow - 1 down to 0. This mirrors the upper half.
    • The logic for printing spaces and stars is the same as the upper half, but it's done in reverse order of rows.

    यह कोड एक खोखला हीरा पैटर्न प्रिंट करने के लिए C++ में है।

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question