C++ Online Compiler
Example: Star Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Star Diamond Pattern #include <iostream> using namespace std; int main() { int size; cout << "Enter the size of the diamond (e.g., 4 for a 7-row diamond): "; cin >> size; // Upper half of the diamond (including the widest row) for (int i = 1; i <= size; i++) { // Step 1: Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Step 2: Print stars for (int k = 1; k <= 2 * i - 1; k++) { cout << "*"; } cout << endl; // Move to the next line } // Lower half of the diamond (excluding the widest row) for (int i = size - 1; i >= 1; i--) { // Step 1: Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Step 2: Print stars for (int k = 1; k <= 2 * i - 1; k++) { cout << "*"; } cout << endl; // Move to the next line } return 0; }
Output
Clear
ADVERTISEMENTS