C++ Online Compiler
Example: Rhombus Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Rhombus Diamond Pattern #include <iostream> using namespace std; int main() { int size; // Step 1: Get user input for the size of the diamond cout << "Enter the size for the diamond (e.g., 3 for a 5-row diamond): "; cin >> size; if (size <= 0) { cout << "Size must be a positive integer." << endl; return 1; } // Step 2: Print the top half of the diamond (including the middle row) for (int i = 0; i < size; ++i) { // Outer loop for rows // Print leading spaces for (int j = 0; j < size - 1 - i; ++j) { cout << " "; } // Print stars for (int k = 0; k < 2 * i + 1; ++k) { cout << "*"; } cout << endl; // Move to the next line after each row } // Step 3: Print the bottom half of the diamond (excluding the middle row) for (int i = 0; i < size - 1; ++i) { // Outer loop for rows (size-1 rows) // Print leading spaces for (int j = 0; j < i + 1; ++j) { cout << " "; } // Print stars for (int k = 0; k < 2 * (size - 1 - i) - 1; ++k) { cout << "*"; } cout << endl; // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS