Code of hollow diamond in cpp language
Computer Science
ScanSolve AI Answer
Step-by-step solution
5 stepsHere'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:
Include Header
The
iostreamheader is included for input/output operations.Get Input
The program prompts the user to enter a
sizefor 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.midRowCalculationmidRowis calculated assize / 2. This represents the row index of the widest part of the diamond.Upper Half
- The outer loop iterates from
i = 0tomidRow. - Leading Spaces: The first inner loop prints the spaces before the stars. The number of spaces decreases as
iincreases. - 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 is2 * i - 1. - Second Star: Another star
*is printed if it's not the top row. - Newline:
std::endlmoves the cursor to the next line.
- The outer loop iterates from
Lower Half
- The outer loop iterates from
i = midRow - 1down to0. 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++ में है।
- The outer loop iterates from
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
More Computer Science Questions
QHttps://www.scribd.com/document/451360592/22317-Sample-question-paper-Data-Structure-Using-C-pdfQI need the discussion part based on these papers That format of discussion of hollow cylinder is…QWrite a program c++ for calendarQFor number two use c programmingQTo write a program based on the provided algorithm. Although the section is labeled "Pascal code"…