C++ Online Compiler
Example: User-Defined Solid Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// User-Defined Solid Diamond Pattern #include <iostream> using namespace std; int main() { int size; // Step 1: Prompt user for input cout << "Enter the size of the diamond (e.g., 3 for a 5-row diamond): "; cin >> size; // Step 2: Input validation (optional but good practice) if (size <= 0) { cout << "Size must be a positive integer." << endl; return 1; // Indicate an error } // Step 3: Print the upper half of the diamond 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; } // Step 4: Print the lower half of the diamond 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; } return 0; }
Output
Clear
ADVERTISEMENTS