C++ Online Compiler
Example: Solid Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Solid Diamond Pattern #include <iostream> using namespace std; int main() { int size = 3; // Define the size of the diamond (number of rows in the upper half) // Step 1: Print the upper half of the diamond (including the middle row) for (int i = 1; i <= size; i++) { // Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Print stars for (int k = 1; k <= 2 * i - 1; k++) { cout << "*"; } cout << endl; // Move to the next line } // Step 2: Print the lower half of the diamond (excluding the middle row) for (int i = size - 1; i >= 1; i--) { // Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Print stars for (int k = 1; k <= 2 * i - 1; k++) { cout << "*"; } cout << endl; // Move to the next line } return 0; }
Output
Clear
ADVERTISEMENTS