C++ Online Compiler
Example: Hollow Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Hollow Diamond Pattern #include <iostream> using namespace std; int main() { int size = 3; // Define the size of the diamond // Step 1: Print the upper half of the hollow diamond for (int i = 1; i <= size; i++) { // Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Print stars for the border for (int k = 1; k <= 2 * i - 1; k++) { if (k == 1 || k == 2 * i - 1) { // First or last star in the row cout << "*"; } else { cout << " "; // Print space in between } } cout << endl; } // Step 2: Print the lower half of the hollow diamond for (int i = size - 1; i >= 1; i--) { // Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Print stars for the border for (int k = 1; k <= 2 * i - 1; k++) { if (k == 1 || k == 2 * i - 1) { // First or last star in the row cout << "*"; } else { cout << " "; // Print space in between } } cout << endl; } return 0; }
Output
Clear
ADVERTISEMENTS